How are you running tests?
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:
-
Somehow all the objects in the collection inside the test have the
tableattribute as null. -
The
wasRecentlyCreatedattribute is set to true while in the controller it's false. -
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?
Please or to participate in this conversation.