Squire's avatar

Table, wasRecentlyCreated and model keys make test fail

If I create resources from my test case and then compare it from what I get in the controller it fails because:

  1. Somehow all the objects in the collection inside the test have the table attribute as null.

  2. The wasRecentlyCreated attribute is set to true while in the controller it's false.

  3. The primary key and foreign key are set to integer in the test case while in the controller they are strings.

What have I try thus far to fix this:

1 ) I don't know what's going on here. I'm using the default configuration that ships with Laravel 7 in the phpunit.xml file, which is this:

<php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value=":memory:"/>
        <server name="MAIL_MAILER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
    </php>

2 ) I tried using the fresh() method in the collection right after I create them in the test case.

/** @test */
    public function a_user_can_see_their_list_of_boards()
    {
        $this->withoutExceptionHandling();

        $user = factory(User::class)->create();
        $boards = factory(Board::class, 10)->create(['user_id' => $user->id]);

        $boards->fresh();

        $this->actingAs($user)
            ->get(route('boards.index'))
            ->assertViewHas('boards', $boards);
    }

3 ) I tried casting the attributes to integer in the Board model.

protected $casts = [
        'id' => 'integer',
        'user_id' => 'integer'
    ];

Couldn't fix any of the problems above. Any suggestion?

0 likes
7 replies
Squire's avatar

Typing vendor\bin\phpunit from the terminal

bugsysha's avatar

What is the exact error you are getting. I'm pretty sure that the collection does not have fresh() method which you are calling.

Squire's avatar

The errors are exactly as they're described in the first part.

PHPUnit is complaining that it is expecting table attribute on model instances to be null but in the controller it is set as the current model table. Also it is expecting the keys to be integers but in the controllers they're strings. The same with wasRecentlyCreated (this is the only one that makes sense to me) that is true but in the controller it's false.

For the moment I've fixed this by querying a new collection instance instead of using the returned value from the factory. But I think there should be a way to avoid this step...

bugsysha's avatar

You are still hiding all the info... And it is working for me. You probably changed something that you forgot about.

mynameiscody07's avatar

Running into this same exact issue on a fresh install of Laravel 7, have no idea what the problem is.

@squire were you able to figure out the problem?

Please or to participate in this conversation.