mercredi 30 octobre 2019

Laravel test lifecycle hook

I have a trait that records activity on the created lifecycle hook. In my test class, if I run any given single test with RefreshDatabase it fails saying the table is empty. If I run the test without RefreshDatabase it passes but then fails the next time for obvious reasons. If I run the entire test class with RefreshDatabase only the first one fails and the rest pass. If I run the entire test class without RefreshDatabase the first one passes and the rest fail because of obvious reasons (duplicate key errors).

Here is one of the tests behaving this way:

    /** @test */
    public function user_can_access_their_own_activity()
    {
        $this->jsonAs($this->user, 'POST', route('team.store'), [
            'display_name' => 'Test Team',
        ])->assertStatus(200);

        $this->assertDatabaseHas('activities', [
            'user_id' => $this->user->getKey(),
            'type' => 'created_team',
        ]);

        $response = $this->jsonAs($this->user, 'GET', route('activity.index'))
            ->assertStatus(200);

        $response->assertJsonFragment([
            'type' => 'created_team',
            'uuid' => $this->user->uuid,
        ]);
    }

If I need to share any more info please let me know. Thanks!

Aucun commentaire:

Enregistrer un commentaire