AssertSee related field

Posted 9 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);


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.

Reply to

Use Markdown with GitHub-flavored code blocks.