Skip to main content
Submitted by lwinmaungmaung on
Laravel Test - Image by Siobhan Dolezal from Pixabay

I am creating a set of Laravel tests for my old project, which has been up since 2017. I started writing the  Laravel tests since the system has been running long with manual testing. With the help of Pest, I can now write multiple Laravel tests fast by sacrificing some pros of PHPUnit. 

When writing a Laravel test, my test fails due to password failure. The user exists but cannot be authenticatable. I tried a lot of ways, but still no hope.

The problem lies under my nose. As it comes from old versions, it has many old writings. My code for setting the password attribute is as follows:

class User{
//pre-codes
public function setPasswordAttribute($value){
    $this->attributes['password']=Hash::make($value);
}
//post-codes
}

The problem lies in the code. It works in the magic method but not with create(). I have to change the codes to the following:

protected $casts = [
    'password' => 'hashed'
];

Viola! My Laravel test passed.