AssertSee related field

Published 1 month 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;
1 month ago (111,370 XP)

If you already have seeded the database then you don't need to use a factory since it's used to create fake data. However This is how I use my factories

 $author1 = factory(App\Author::class)->create();

and my AuthorFactory.php looks like this


use Faker\Generator as Faker;

$factory->define(App\Author::class, function (Faker $faker) {
    return [
        'first_name' => $faker->firstName(),
        'last_name' => $faker->lastName()

I recommend that you read this

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