在运行测试套件时重设自动增量(id)?

时间:2019-07-10 18:48:31

标签: laravel phpunit

让我说我有这个测试:

class test extends TestCase
{
    use RefreshDatabase;

    /** @test */
    public function test_ids_example()
    {
        $courses = factory(Post::class, 3)->create();
        $this->assertEquals([1, 2, 3], $courses->pluck('id')->toArray());
    }

    /** @test */
    public function test_ids_example_2()
    {
        $courses = factory(Post::class, 4)->create();
        $this->assertEquals([1, 2, 3, 4], $courses->pluck('id')->toArray());
    }
}

当我逐个运行测试时,它通过了。 但是当我运行整个测试文件时,出现此错误:

test::test_ids_example_2
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
 Array (
-    0 => 1
-    1 => 2
-    2 => 3
-    3 => 4
+    0 => 4
+    1 => 5
+    2 => 6
+    3 => 7
 )

因为即使我添加了“ RefreshDatabase ”特征,测试也不会重置自动增量“ id”。如何解决呢?如何为每个测试保留ID?

0 个答案:

没有答案