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));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常