【问题标题】:Laravel Dusk: Migrate and Seed Testing DB OnceLaravel Dusk:迁移和种子测试数据库一次
【发布时间】:2020-06-04 12:12:39
【问题描述】:

是否可以运行一次迁移和播种并且不刷新测试方法之间的测试数据库?

我有几个相互依赖的测试功能,我不想在每个测试之前和之后迁移数据库并为其提供种子在一个测试文件中

示例:

<?php

namespace Tests\Browser;

use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Carbon\Carbon;

class AdminTest  extends DuskTestCase
{
  use DatabaseMigrations;

  /**
   * Define hooks to migrate the database before and after each test.
   *
   * @return void
   */
   protected function setUp(): void
    {
        parent::setUp();
        $this->artisan('db:seed', ['--class' => 'DatabaseSeeder']);
    }

    public function testAdminCanLogin()
    {
    }

    /* Create New ticket */
    public function testAdminCreateTicket()
    { 
    }

    /* View the first ticket */
    public function testAdminViewTicket()
    {
    }

    /* Edit the first ticket */
    public function testAdminEditTicket()
    {
    }

    /* Assign the first Ticket to an Agent */
    public function testAdminAssignTicketToAgent()
    {
    }

    /* Unassign the first Ticket from Agent */
    public function testAdminUnassignAgentFromTicket()
    {
    }

    /* Delete the first ticket */
    public function testAdminDeleteTicket()
    {
    }

    /* Restore the first ticket */
    public function testAdminRestoreTicket()
    { 
    }

}

【问题讨论】:

    标签: laravel-dusk laravel-migrations laravel-seeding


    【解决方案1】:

    不要使用数据库迁移。 只是: $this->artisan('migrate:fresh'); $this->artisan('db:seed'); 喜欢:

    public function setUp(): void
        {
            $this->appUrl = env('APP_URL');
            parent::setUp();
            $this->artisan('migrate:fresh');
            $this->artisan('db:seed');
        }
    

    在您的第一次浏览器测试中

    【讨论】:

      【解决方案2】:

      是的,你可以这样做

      protected static $migrationRun = false;
      
      public function setUp(): void{
          parent::setUp();
      
          if(!static::$migrationRun){
              $this->artisan('migrate:refresh');
              $this->artisan('db:seed');
              static::$migrationRun = true;
          }
      }
      

      将它包含在你的黄昏测试课程中。 setUp 方法在每个测试方法之前运行,如果迁移已运行一次,则不会再次运行。

      【讨论】:

      • 我刚看到你的回答,它对我不起作用第二次测试没有成功,它重定向到在票证页面上插入的登录页面以创建票证,当我复制登录代码时第二次测试(创建票证)它没有成功,好像用户表被回滚了!
      • @Learner 你只需要删除 use DatabaseMigrations 特征。所以它不会为每个测试重置数据库。
      猜你喜欢
      • 2017-07-10
      • 2018-06-12
      • 2019-10-29
      • 2018-05-28
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 2018-07-18
      • 2014-10-07
      相关资源
      最近更新 更多