AssertSee related field

Posted 2 months ago by Benjamest

Hi all,

I'm trying to assert that the related field name of room->name is visible however I am getting the following error:

InvalidArgumentException: Unable to locate factory with name [default] [App\Models\Room].

I imagine the error is because the factory is not eager loading the relation but I'm not sure how best to eager load for the test as per my controller. My test function:

    /** @test */
    public function a_user_can_view_a_booking()
    {
        $booking = factory('App\Models\Booking')->create();
        $response = $this->get('/booking/'.$booking->id);

        $response->assertSee($booking->id);
        $response->assertSee($booking->room->name);
        $response->assertSee($booking->date);
        $response->assertSee($booking->start_time);
        $response->assertSee($booking->end_time);
    }
}

The room table is already pre-seeded and the factory works correctly. I can see the room name when I view the page after populating in tinker but unsure how to write the test. The factory consists of:

'room_id' => getInstanceOf(Room::class),

function getInstanceOf($class, $returnIdOnly = true) {
        $instance = $class::inRandomOrder()->first() ?? factory($class)->create();
        return $returnIdOnly ? $instance->id : $instance;
    }

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.