我有一个密码修改器:
/**
* Mutator for setting the encryption on the user password.
*
* @param $password
*/
public function getPasswordAttribute($password)
{
$this->attributes[ 'password' ] = bcrypt($password);
}
我正在尝试测试:
/**
* A basic check of password mutator.
*
* @return void
*/
public function testCheckPasswordEncryptionUserAttribute()
{
$userFactory = factory('Project\User')->create([
'password' => 'test'
]);
$user = User::first();
$this->assertEquals(bcrypt('test'), $user->password);
}
当测试运行时我得到这个错误:
1) UserTest::testCheckPasswordEncryptionUserAttribute
Failed asserting that null matches expected '$2y$10$iS278efxpv3Pi6rfu4/1eOoVkn4EYN1mFF98scSf2m2WUhrH2kVW6'.
测试失败后,我尝试 dd() 密码属性,但这也失败了。我的第一个想法是这可能是一个批量分配问题(刚刚阅读过),但是密码在 $fillable 中(这很有意义,它会在那里),然后我注意到 User 类中的 $hidden 也是如此,但是之后在文档中阅读相关内容,并删除 $hidden 的密码索引,当您尝试访问密码属性时,它仍然会产生空值。
您将如何对这个 mutator 进行单元测试,或者我错过了什么?