assertSee do not validate on collection - why ? Hello friends,
Can you help me a little bit, please? I am really trying to use test but I don't know why my test is not passing
public function test_show_list_of_quizzes_on_the_homepage()
{
$quizzes = factory(Quiz::class,5)->create();
$response = $this->get('/home')->assertStatus(200)
->assertViewIs('home')
->assertSee('Quizzes')
->assertSee($quizzes->title);
Errors: 1 Exception Property[title] does not exist on this collection instance.
Thank you
$quizzes is a collection object holding 5 quizes..... ofcourse it doesnt know how to show the title (of which quiz do you want to check the title?)
Try this to check for the first and last quiz's title:
$quizzes = factory(Quiz::class,5)->create();
$response = $this->get('/home')->assertStatus(200)
->assertViewIs('home')
->assertSee('Quizzes')
->assertSee($quizzes->first()->title)
->assertSee($quizzes->last()->title);
Thank you very much lostdreamer_nl it worked and I used assertViewHas('quizzes') and passed also :)
$quizzes = factory(Quiz::class,5)->create();
$response = $this->get('/home')->assertStatus(200)
->assertViewIs('home')
->assertSee('Quizzes')
->assertViewHas('quizzes');
Just watch out, you're now creating a few new quizes, then opening the main page and checking to see the word 'quizzes' not 1 of the newly created $quizzes.
You can use $response->original->getData()['quizzes']to get the collection that is passed into the view as well. That way, you can do additional tests to check that the collection is what you are expecting it to be, and not just that it is passed through
Please sign in or create an account to participate in this conversation.