【问题标题】:Laravel persisting only during duration of unit testLaravel 仅在单元测试期间持续存在
【发布时间】:2020-08-24 14:21:29
【问题描述】:

我正在对 API 端点进行单元测试,我有这样的事情:

public function testPostService()
{
    $response = $this->json('POST', '/api/v1/service',[
            "id"=> "someId",
            "number" => 123
    ]);
    $response
        ->assertStatus(201)
        ->assertJson([
            'created' => true,
        ]);
}

id 是一个唯一值,每次运行测试时都会出现问题。我必须清空数据库或更改 id 值。

有什么方法可以只在测试期间保留持久值?

我的phpunit.xml是标准的

【问题讨论】:

  • 你做的不是unitTest,而是功能/api测试。

标签: laravel unit-testing laravel-5 phpunit


【解决方案1】:

有一个名为RefreshDatabase 的特征会在每次测试完成后刷新数据库。只需将其添加到您的测试用例类的类中即可。

这能解决您的问题吗?

【讨论】:

    猜你喜欢
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 2014-03-29
    • 2015-02-27
    • 2017-07-06
    • 2022-01-19
    • 2017-06-02
    相关资源
    最近更新 更多