如何断言 Laravel 8 中没有键的错误
How to assert errors with no keys in Laravel 8
我正在为我的应用程序编写测试。
这是我的测试:
/** @test */
public function change_password_password_must_be_the_same()
{
$old_password = 'password';
$different_password = 'password1';
$new_password = 'new password';
$user = User::factory()->create([
'password' => Hash::make($old_password)
]);
$this->actingAs($user)
->patch("/profile/{$user->id}/change-password", [
'password' => $different_password,
'new_password' => $new_password,
'confirm_new_password' => $new_password
])
// How do I assert this?
->assertSessionHasErrors([
'errors' => 'Password is incorrect.'
]);
}
如何在没有键的情况下断言错误?就像我在正在测试的代码中执行此操作一样?
if (!Hash::check($password, $user->password)) {
return redirect()
->route('users.edit', ['user' => $user])
->withErrors('Password is incorrect');
}
我终于找到了答案。它是:
$errors = session('errors');
$messages = $errors->getBag('default')->getMessages();
$this->assertEquals('Password is incorrect.', $messages[0][0]);
我正在为我的应用程序编写测试。
这是我的测试:
/** @test */
public function change_password_password_must_be_the_same()
{
$old_password = 'password';
$different_password = 'password1';
$new_password = 'new password';
$user = User::factory()->create([
'password' => Hash::make($old_password)
]);
$this->actingAs($user)
->patch("/profile/{$user->id}/change-password", [
'password' => $different_password,
'new_password' => $new_password,
'confirm_new_password' => $new_password
])
// How do I assert this?
->assertSessionHasErrors([
'errors' => 'Password is incorrect.'
]);
}
如何在没有键的情况下断言错误?就像我在正在测试的代码中执行此操作一样?
if (!Hash::check($password, $user->password)) {
return redirect()
->route('users.edit', ['user' => $user])
->withErrors('Password is incorrect');
}
我终于找到了答案。它是:
$errors = session('errors');
$messages = $errors->getBag('default')->getMessages();
$this->assertEquals('Password is incorrect.', $messages[0][0]);