faraz73's avatar

findOrFail() works on test but not in action

Hi , im learning livewire so just write this test :

/**
     * @test
     */
    public function if_a_wrong_query_string_has_been_set_no_data_should_be_returned()
    {
        Livewire::withQueryParams(['post_id',rand()])
            ->test('posts')
            ->assertSee('title','')
            ->assertSee('text','')
            ->assertSee('save');
    }

this test returns green! .. but in action this error comes up :

No query results for model [App\Models\Post] 444 (View: E:\---\livewire_CRUD\resources\views\welcome.blade.php)

I have implemented mount() in component like so :

public $posts, $post, $title, $text, $post_id;
protected $queryString = ['post_id'];

public function mount()
    {
        if ($this->post_id) {
            $this->queryString['post_id'] = $this->post_id;
            $this->edit($this->queryString['post_id']);
        }
    }

and edit method is :

public function edit($post_id)
    {
        $post = Post::findOrFail($post_id);
        $this->editPost = true;
        $this->title = $post->title;
        $this->text = $post->text;
        $this->post_id = $post->id;
        $this->resetErrorBag();
    }
0 likes
2 replies
freemium's avatar
public function edit(Post $post)
    {
return $post
       
    }
faraz73's avatar

@freemium funny if do this .. test will pass but in action this error comes up :

Argument 1 passed to App\Http\Livewire\Posts::edit() must be an instance of App\Models\Post

Please or to participate in this conversation.