如何为 Laravel 更新密码表单编写 phpunit 测试?
这是我的测试:
$user = \factory(\App\User::class)->create(['password' => \Hash::make('password')]);
$this->actingAs($user);
$response = $this->call('PUT', '/user/update-password', array(
'_token' => csrf_token(),
'current_password' => 'password',
'new_password' => 'newone',
'repeat_new_password' => 'newone',
));
$response->assertStatus(302);
$this->assertDatabaseHas('users', ['name' => $user->name, 'password' => \Hash::make('newone')]);
Run Code Online (Sandbox Code Playgroud)
密码控制器正在保存新密码,如下所示:
....
$user->password = \Hash::make($request->new_password);
$user->save();
....
Run Code Online (Sandbox Code Playgroud)
我收到错误:“无法断言表 [user] 中的行与属性匹配”
更新密码表单工作正常,因为我可以使用更新的密码登录。我猜想: $this->assertDatabaseHas('users', ['name' => $user->name, 'password' => \Hash::make('newone')]); 正在创建与我的密码控制器不同的密码。
任何想法,这里出了什么问题?
尝试使用ask()函数为laravel php artisan命令编写测试.我之前从未使用过嘲弄,但是当我尝试运行测试时,它会冻结,所以我猜,我做错了什么.
MyCommand.php:
public function handle()
{
$input['answer1'] = $this->ask('Ask question 1');
$input['answer2'] = $this->ask('Ask question 2');
$input['answer3'] = $this->ask('Ask question 3');
//--- processing validation
$validator = Validator::make($input, [
'answer1' => 'required',
'answer2' => 'required',
'answer3' => 'required',
]);
if ($validator->fails()) {
// processing error
}
} else {
// saving to DB
}
}
Run Code Online (Sandbox Code Playgroud)
我的单元测试:
$command = m::mock('\App\Console\Commands\Questions');
$command->shouldReceive('ask')
->andReturn('Answer 1')
->shouldReceive('ask')
->andReturn('Answer 2')
->shouldReceive('ask')
->andReturn('Answer 3')
$this->artisan('myCommand:toRun');
$this->assertDatabaseHas('myTable', [
'question1' => 'answer1'
]);
Run Code Online (Sandbox Code Playgroud)
//