简体   繁体   English

Laravel 5:Model-> fill()在单元测试中忽略$ fillable属性

[英]Laravel 5: Model->fill() ignores $fillable property in unit tests

I have a user controller with the following validation rules: 我有一个用户控制器,其中包含以下验证规则:

public function store(Request $request)
{
    ...
    $this->validate($request, [
        'name' => 'required',
        'email' => 'email|required|unique:users',
        'password' => 'confirmed|max:32|min:8|required',
        'roles' => 'exists:roles,id|required',
    ]);

    $user = new User();
    $user->fill($request->all());
   ...
}

My User.php model defines the fillable properties as: 我的User.php模型将可填充属性定义为:

protected $fillable = ['name', 'email'];

To pass the confirmed validation, I have to send both password and password_confirmation fields in the POST request. 要通过confirmed验证,我必须在POST请求中发送passwordpassword_confirmation字段。

During development everything works fine, but in unit tests I'm getting a database error. 在开发过程中一切正常,但在单元测试中,我遇到了数据库错误。 It tries to insert data into a password_confirmation column. 它尝试将数据插入password_confirmation列。 It's like it ignores the $fillable array. 它就像忽略了$fillable数组。

I know about the "laravel losts event handlers between tests" bug/issue ( https://github.com/laravel/framework/issues/1181 ). 我知道“测试之间的laravel losts事件处理程序”错误/问题( https://github.com/laravel/framework/issues/1181 )。 So I think that maybe I'm missing to call some model function aside from Model::boot() (I'm calling User::boot() in the test's setUp() function). 所以我想也许我不想在Model::boot()之外调用一些模型函数(我在测试的setUp()函数中调用User::boot() )。

Thanks, 谢谢,

Edit 编辑

Reading the Model.php source, I've found that someone is calling Model::unguard() https://github.com/laravel/framework/blob/5.1/src/Illuminate/Database/Eloquent/Model.php#L2180 after the setUp() function and before the test. 阅读Model.php源代码,我发现有人正在调用Model::unguard() https://github.com/laravel/framework/blob/5.1/src/Illuminate/Database/Eloquent/Model.php#L2180在setUp()函数之后和测试之前。 If I call User::reguard() at the beggining of the test it passes, but (I don't know why), the unguard() and reguard() functions get called multiple times and the test gets really slow. 如果我在测试开始时调用User::reguard() ,但是(我不知道为什么),unguard()和reguard()函数被多次调用,测试变得非常慢。

发现问题:v5.0.x中的基本播种器只叫做Model :: unguard()( https://github.com/laravel/laravel/blob/v5.0.22/database/seeds/DatabaseSeeder.php#L15 )更新v5.1.x并添加了对Model :: reguard()的调用( https://github.com/laravel/laravel/blob/v5.1.0/database/seeds/DatabaseSeeder.php#L19)(I正在使用v5.0.22)。

Fillable only applies to MassAssignment. 可填充仅适用于MassAssignment。 When you're creating a new instance, like what you're doing above, you're not triggering the mass assignment event. 当您创建新实例时,就像上面所做的那样,您不会触发质量分配事件。

You could do something like this: If you're creating the user anyway, you might as well do: 你可以这样做:如果你正在创建用户,你可能会做:

$user = User::create($request->all);

If you just want to instantiate the user, without persisting the data, you could do something like this: 如果您只想实例化用户而不保留数据,则可以执行以下操作:

$user = new User($request);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM