首頁>Program>source

我建立了一个測試.它不斷出現此錯誤:

  FAIL  Tests\Feature\WorkfieldTest
  ✓ index should be accessible by internal
  ⨯ index should be accessible by customer
  ⨯ index should be accessible by employee
  ⨯ index should not be accessible by guest
   Illuminate\Database\QueryException 
  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`myapp`.`workfields`, CONSTRAINT `workfields_specialization_id_foreign` FOREIGN KEY (`specialization_id`) REFERENCES `specializations` (`id`) ON DELETE CASCADE) (SQL: insert into `workfields` (`specialization_id`, `server_generated`, `updated_at`, `created_at`) values (1, 1, 2020-11-18 18:55:50, 2020-11-18 18:55:50))

我已经用谷歌搜尋並在各地阅讀類似的問题,但仍然找不到合適的答案.我已经定義並致電 setUp的播種機 ,尊重表格的順序.測試方法的第一次呼叫已通過,其餘均失败.我看到该錯誤有一个插入呼叫,与此同時,我的測試用例除了从中获取並計數資料外,什麼也不做.那麼,這裏發生了什麼?

Test

class WorkfieldTest extends TestCase
{
    use RefreshDatabase;
    private $internal;
    private $customer;
    private $employee;
    private $count;
    public function setUp(): void
    {
        parent::setUp();
        $this->app->make(PermissionRegistrar::class)->registerPermissions();
        $this->seed(RolesAndPermissionSeeder::class);
        $this->seed(SpecializationSeeder::class);
        // dd(Specialization::count()); -> 68
        $this->seed(WorkfieldSeeder::class);
        $this->internal = User::factory()->create()->assignRole('internal');
        $this->customer = User::factory()->create()->assignRole('customer');
        $this->employee = User::factory()->create()->assignRole('employee');
        $this->count = Workfield::count();
    }
    public function testIndexShouldBeAccessibleByInternal()
    {
        $this->actingAs($this->internal, 'api')
            ->getJson(route('workfields.index'))
            ->assertOk()
            ->assertJsonCount($this->count);
    }
    public function testIndexShouldBeAccessibleByCustomer()
    {
        $this->actingAs($this->customer, 'api')
            ->getJson(route('workfields.index'))
            ->assertOk()
            ->assertJsonCount($this->count);
    }
    public function testIndexShouldBeAccessibleByEmployee()
    {
        $this->actingAs($this->employee, 'api')
            ->getJson(route('workfields.index'))
            ->assertOk()
            ->assertJsonCount($this->count);
    }
    public function testIndexShouldNotBeAccessibleByGuest()
    {
        $this->getJson(route('workfields.index'))
            ->assertUnauthorized();
    }
}

Migration

public function up()
{
    Schema::create('workfields', function (Blueprint $table) {
        $table->id();
        $table->foreignId('specialization_id')->constrained()->onDelete('cascade');
        $table->boolean('server_generated')->default(false);
        $table->timestamps();
    });
}
public function down()
{
    Schema::dropIfExists('workfields');
}
最新回復
  • 9天前
    1 #

    在使用記憶體中的sqlite資料庫時,通過取消註釋標準 phpunit.xml中的以下两行,測試檔案可以正確執行 Laravel安裝附帶的

    
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value=":memory:"/>
    

    但是,在针對MySQL进行測試時,失败了.可能在每次測試之前執行的種子服務器是一个問题-因為您的種子計算機包含从json檔案中获取資料,然後在執行 Model::create()之前將它们轉換為關聯的陣列 .在記憶體資料庫中,不会遇到這種問题,但是在MySQL中,問题会以某種方式出現。

    有两種方法可以解決此問题.第一種最簡單的方法是包括在 DatabaseSeeder中執行所有播種機的呼叫 類,然後設置 $seed的值 在需要種子資料的測試類上將屬性設置為true-在此處更多資訊

    
    class WorkfieldTest extends TestCase
    {
       use RefreshDatabase;
       /**
        * Indicates whether the database should be seeded before each test.
        *
        * @var bool
        */
        protected $seed = true;
       //... tests
    }
    

    另一種選擇是通過在類上設置一个靜態變數並使用它来控製某些播種器對於该類中的所有測試仅執行一次,从而在所有測試之前仅使用一次从json檔案中获取的資料執行播種器. / p>

    
    class WorkfieldTest extends TestCase
    {
        use RefreshDatabase;
    
        protected static $initialized = false;
        protected function setUp(): void
        {
            parent::setUp();
            $this->app->make(PermissionRegistrar::class)->registerPermissions();
            $this->seed(RolesAndPermissionSeeder::class);
            if (!static::$initialized) {
                $this->seed([SpecializationSeeder::class, WorkfieldSeeder::class]);
                static::$initialized = true;
            }
        }
       //... tests
    }
    

    以上两種方法中的任何一種都可以使測試在MySQL / Sqlite上執行而不会出現任何錯誤。

  • python:如何提取具有混合值的行
  • vba:包含SUM的Excel IF語句