Home > database >  Laravel Livewire testing
Laravel Livewire testing

Time:12-01

I am looking forward to learn Livewire and its test as well. Now I have created component for registering users and it is working fine, but when I try to do a test I get this error: Failed asserting that an array has the key 'redirect'.

Here are the parts of the code:

RegisterTest

class RegisterTest extends TestCase
{
    use RefreshDatabase;

/** @test */
public function can_register()
{
    Livewire::test('auth.register')
    ->set('name', 'user')
    ->set('email', '[email protected]')
    ->set('password', 'secret')
    ->set('password_confirmation', 'secret')
    ->call('register')
    ->assertRedirect('/');
}
 }

Component

public $name = '';
public $email = '';
public $password = '';
public $password_confirmation = '';

public function register()
{
    $data = $this->validate([
        'name' => 'required|string',
        'email' => 'required|email',
        'password' => 'required|confirmed|min:8|string'
    ]);

    User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);

    return redirect('/');
}

CodePudding user response:

This is pretty easy.

Add the following file:

->assertHasNoErrors(['name', 'email', 'password']);

before

->assertRedirect('/');

line and you will see it will fail. This is because your validation tells password should be minimum 8 characters and in your test it is 6 (you used secret as password)

  • Related