vincent15000's avatar

Livewire test for an update do not work ?

Hello,

I'm trying Livewire tests, but I'm lost.

The update is not done and the result is false.

What is wrong in my code ?

/** @test  */
public function admin_can_update()
{
    $user = User::where('admin', true)->first();
    $this->actingAs($user);
    $author = Author::
        where('firstname', 'Nom')
        ->where('lastname', 'Prénom')
        ->where('website', 'https://www.nom-prenom.fr/')
        ->first();
    Livewire::test(AdminAuthorForm::class, ['author' => $author])
        ->set('updateMode', true)
        ->set('author.firstname', 'Mon nom')
        ->set('author.lastname', 'Mon prénom')
        ->set('author.website', 'https://www.nomprenom.fr/')
        ->call('save');
    $result = Author::
        where('firstname', 'Mon nom')
        ->where('lastname', 'Mon prénom')
        ->where('website', 'https://www.nomprenom.fr/')
        ->exists();
    $this->assertTrue($result);
}

Thanks for your help ;).

0 likes
9 replies
Nakov's avatar

You say the Update is NOT done and the result is false how do you expect it to be true when the update is NOT done?

1 like
vincent15000's avatar

@Nakov Ok I understand your answer ... the update should work, so I don't understand why my update does not work.

I just read this error message, but it has no sense for me because my property is Author typed.

Typed property App\Http\Livewire\Admin\AdminAuthorForm::$author must be an instance of App\Models\Author, null used
Nakov's avatar

@vincent15000 no that's not true. In a test environment you usually use different database than the one you use for local testing through the browser. So in tests you usually should create the authors using a Factory

$author = Author::
        where('firstname', 'Nom')
        ->where('lastname', 'Prénom')
        ->where('website', 'https://www.nom-prenom.fr/')
        ->first();

->first() returns null if it cannot find the record, so you think it is an Author but that one does not exists.

Read more here: https://laravel.com/docs/8.x/database-testing#creating-models-using-factories

1 like
vincent15000's avatar

@Nakov Ok that's right, but I have already tested and dd($author) displays the author as expected. That's why I don't understand why the update doesn't work, whereas it works fine via the browser.

Nakov's avatar
Nakov
Best Answer
Level 73

@vincent15000 try instead of ->first() use ->firstOrFail() and tell me if it blows up or it works ?

You tried dd() in the test or in the browser?

Do you have $author in the mount() method as a param, or just as a property? Try setting it:

Livewire::test(AdminAuthorForm::class, ['author' => $author])
	->set('author', $author)..

Seeing just your test is hard to say what could be wrong, but there are tons of ways to debug this.

1 like
vincent15000's avatar

@Nakov I have already tested what you suggest me here. I get an error message saying that there is no validation rules for $author. Yes I initialize the $author in the mount() method. But I effectively have no validation rules for $author because I don't need it in my form.

vincent15000's avatar

@Nakov Well ... it looks like my $author property is not passed (but I'm not sure) to the Livewire test method.

vincent15000's avatar

@Nakov Hello that's not exactly the best answer, but this answer helped me to find the problem. Thank you ;).

Please or to participate in this conversation.