首页 > 解决方案 > Laravel 测试生命周期钩子

问题描述

我有一个在创建的生命周期钩子上记录活动的特征。在我的测试类中,如果我运行任何给定的单个测试 RefreshDatabase它会失败,说表是空的。如果我在没有 RefreshDatabase通过的情况下运行测试,但由于明显的原因下次失败。如果我运行整个测试类 RefreshDatabase只有第一个失败,其余通过。如果我在没有 RefreshDatabase第一个通过的情况下运行整个测试类,而其余的由于明显的原因(重复键错误)而失败。

这是以这种方式运行的测试之一:

    /** @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,
        ]);
    }

如果我需要分享更多信息,请告诉我。谢谢!

标签: phpdatabaselaraveltestinglifecycle-hook

解决方案


推荐阅读