在Laravel测试期间创建和删除表

时间:2018-06-12 14:09:24

标签: php laravel phpunit

我在Laravel 5.6应用程序中创建了一个特性。该特征称为Projectable。这个特性是由Eloquent模型使用的。为了测试这个特性,我想创建一个ProjectableStub模型用于测试。但是,由于这是一个Eloquent模型,它需要一个表。

我想简单地创建并删除一个表以供测试。但是,当我执行此操作时,似乎有关RefreshDatabase功能的中断。为了演示,我只是运行两个测试,两个测试都尝试使用Product创建id = 1模型。由于正在使用RefreshDatabase特性,这应该可以正常工作。并且,在下面的示例中,它确实:

<?php

namespace Tests\Feature;

use App\Product;
use Tests\TestCase;
use App\Concerns\Projectable;
use Illuminate\Support\Facades\Schema;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Database\Eloquent\Model as Eloquent;

class ProjectableTest extends TestCase
{
    use RefreshDatabase;

    public function setUp()
    {
        parent::setUp();

        //$this->createStubTable();
    }

    /**
     * @test
     */
    public function example_first_test()
    {
        factory(Product::class)->create(['id' => 1]);
    }

    /**
     * @test
     */
    public function example_second_test()
    {
        factory(Product::class)->create(['id' => 1]);
    }

    public function tearDown()
    {
        //$this->dropStubTable();

        parent::tearDown();
    }

    private function createStubTable()
    {
        Schema::create('stubs', function ($table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    private function dropStubTable()
    {
        Schema::dropIfExists('stubs');
    }
}

class ProjectableStub extends Eloquent
{
    use Projectable;

    protected $table = 'stubs';

    protected $guarded = [];
}

但是,只要我取消注释这两行,以便创建并删除stubs表,我就会收到一个SQL错误,表明正在使用重复的ID:

<?php

namespace Tests\Feature;

use App\Product;
use Tests\TestCase;
use App\Concerns\Projectable;
use Illuminate\Support\Facades\Schema;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Database\Eloquent\Model as Eloquent;

class ProjectableTest extends TestCase
{
    use RefreshDatabase;

    public function setUp()
    {
        parent::setUp();

        $this->createStubTable();
    }

    /**
     * @test
     */
    public function example_first_test()
    {
        factory(Product::class)->create(['id' => 1]);
    }

    /**
     * @test
     */
    public function example_second_test()
    {
        factory(Product::class)->create(['id' => 1]);
    }

    public function tearDown()
    {
        $this->dropStubTable();

        parent::tearDown();
    }

    private function createStubTable()
    {
        Schema::create('stubs', function ($table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    private function dropStubTable()
    {
        Schema::dropIfExists('stubs');
    }
}

class ProjectableStub extends Eloquent
{
    use Projectable;

    protected $table = 'stubs';

    protected $guarded = [];
}
  

1)测试\ Feature \ ProjectableTest :: example_second_test   Illuminate \ Database \ QueryException:SQLSTATE [23000]:完整性   约束违规:1062重复输入&#39; 1&#39;关键&#39; PRIMARY&#39;

有谁知道为什么在测试中创建和删除表会导致此问题?有没有更好的方法来解决这个问题?也许某种方法可以在运行时为这个新表添加迁移?

1 个答案:

答案 0 :(得分:1)

我认为这可能就是答案:

https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html

创建表会导致predict(x_glm, newdata = data2, type ="response")创建的活动事务自动提交。

暂时使存根表成功。这也意味着我不需要放弃桌子,因为它会自动发生:

RefreshDatabase

到目前为止似乎工作得很好。