模拟Laravel自定义验证规则类不起作用 [英] Mocking Laravel custom validation rule class not working

查看:358
本文介绍了模拟Laravel自定义验证规则类不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在表单提交中实施新的自定义验证规则.但是我想在单元测试中绕过验证规则.下面是验证规则和单元测试类的简化.我想念什么?

I'm implementing a new custom validation rule in form submit. But I want to bypass the validation rule in unit testing. Below is the simplified of the validation rule and unit test class. What am I missing?

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class Captcha implements Rule
{
    public function passes($attribute, $value)
    {
        // assuming will always return false in testing
        // works fine when true
        return false; 
    }

    public function message()
    {
        return 'Captcha error! Try again later or contact site admin.';
    }
}

use Tests\TestCase;
use App\Rules\Captcha;

class RegistrationTest extends TestCase {

    public test_user_registration()
    {
        $this->mock(Captcha::class, function ($mock) {
            $mock->shouldReceive('passes')->andReturn(true);
        });

        $response = $this->post(route('tenant.register'), [
            'g-recaptcha-response' => 1,
            'email' => 'user@example.com',
            'password' => 'secret',
        ]);

        $this->assertEquals(1, User::all()->count());
     }
} 

同时包含FormRequestController文件

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Rules\Captcha;

class NewUserRequest extends FormRequest {

    public function rules()
    {
        return [
            'name' => ['required', new Captcha]
        ];
    }
}

...

public function postRegister(NewUserRequest $request) {

...

编辑II:似乎是Laravel本身的错误;

EDIT II: seems like a bug in Laravel itself;

  • https://github.com/laravel/framework/issues/28468
  • https://github.com/laravel/framework/issues/19450
  • https://github.com/laravel/framework/issues/25041

尝试了提供的解决方案,但仍然无法正常工作

tried the provided solutions but still not working

推荐答案

必须通过Laravels服务容器实例化该类,以便对其进行模拟.最好的方法(在这种情况下)是将new Captcha更改为app(Captcha::class):

The class must be instantiated through Laravels service container in order for it to be mocked. The best way to accomplish this (in this situation) is to simply change new Captcha to app(Captcha::class):

命名空间App \ Http \ Requests;

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Rules\Captcha;

class NewUserRequest extends FormRequest {

    public function rules()
    {
        return [
            'name' => ['required', app(Captcha::class)]
        ];
    }
}

我建议您不要告诉规则本身根据环境来更改其行为,因为当您试图弄清验证码在开发环境中什么都不做的时候,这可能会引起混乱.生产失败.

I would advise against telling the rule itself to change its behaviors based on environments as that could result in a bit of confusion down the line when you are trying to figure out why the captcha doesn't do anything in dev environments but is failing in production.

这篇关于模拟Laravel自定义验证规则类不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆