Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Disciple's avatar

Livewire set() method doesn't work in tests

What I have in my test:

$news = News::factory()->create();

Livewire::test(Show::class)
            ->set('news', $news)
            ->assertSet('news', $news)
            ->assertViewHas('news', fn($news) => $news instanceof News)
            ->assertSee($news->title);

Show::class:

    public News $news;

    public function mount(News $news): void
    {
        $this->news = $news;
    }

    public function render(): View
    {
        return view('livewire.news.show', [
            'news' => $this->news,
        ]);
    }

But somewhy test is failing because in reality I have empty News $news but I called set('news', $news) in my test. In the result of course my view is empty also just html without $news variable inside, so the question is how to set news within test? Thanks in advance.

0 likes
1 reply

Please or to participate in this conversation.