简体   繁体   中英

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:

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

To pass the confirmed validation, I have to send both password and password_confirmation fields in the POST request.

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. It's like it ignores the $fillable array.

I know about the "laravel losts event handlers between tests" bug/issue ( 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).

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. 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.

发现问题: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. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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