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

webRookie's avatar

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

0 likes
4 replies
lostdreamer_nl's avatar
Level 53

$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);
1 like
webRookie's avatar

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');
lostdreamer_nl's avatar

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.

alanholmes's avatar

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

1 like

Please or to participate in this conversation.