dongzhan5943 2015-06-21 19:27
浏览 32
已采纳

Laravel 5.1无法对用户密码变异器进行测试

I have a password mutator:

/**
 * Mutator for setting the encryption on the user password.
 *
 * @param $password
 */
public function getPasswordAttribute($password)
{
    $this->attributes[ 'password' ] = bcrypt($password);
}

That I'm trying to test:

/**
 * 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);
}

That when the test runs I get this error:

1) UserTest::testCheckPasswordEncryptionUserAttribute
Failed asserting that null matches expected '$2y$10$iS278efxpv3Pi6rfu4/1eOoVkn4EYN1mFF98scSf2m2WUhrH2kVW6'.

After the test failed I attempted to dd() the password property, but that also failed. My first thought was this might be a mass assignment issue (having just read about that), but password is in $fillable (which makes sense that it would be there), then I noticed $hidden in the User class as well, but after reading about that in the docs, and also removing the password index for $hidden it still produces a null when you try to access the password property.

How would you unit test this mutator, or what have I missed?

  • 写回答

1条回答 默认 最新

  • dongshi1207 2015-06-23 03:20
    关注

    You just have to change "get" to "set" in your method name.

    Methods starting with "get" are accessors. These are not supposed to change the field / attribute value but return a "mutated" value (yours returns nothing that's why you get null).

    Methods starting with "set" are designed to change the value of the field (mutators) and this seems to be exactly what you need.

    http://laravel.com/docs/5.0/eloquent#accessors-and-mutators

    /**
     * Mutator for setting the encryption on the user password.
     *
     * @param $password
     */
    public function setPasswordAttribute($password)
    {
        $this->attributes['password'] = bcrypt($password);
    }
    

    You can make "password" hidden, because this won't affect your test.

    P.S. If I'm not wrong, factory('...')->create() returns an instance of a newly created model (\Illuminate\Database\Eloquent\Model), so you don't have to do User::first():

    /**
     * A basic check of password mutator.
     *
     * @return void
     */
    public function testCheckPasswordEncryptionUserAttribute()
    {
        $user = factory('Project\User')->create(['password' => 'test']);
    
        $this->assertTrue(Hash::check('test', $user->password));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化