dimanche 13 septembre 2020

How to assert errors with no keys in Laravel 8

I'm writing tests for my application.

Here is my test:

/** @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.'
        ]);
}

How do I assert errors with no keys? Like when I do this in the code I'm testing?

if (!Hash::check($password, $user->password)) {
    return redirect()
        ->route('users.edit', ['user' => $user])
        ->withErrors('Password is incorrect');
}

Aucun commentaire:

Enregistrer un commentaire