我有一个Laravel 5.4项目,正在尝试使用Dusk进行一些测试。我想在运行测试之前重置,迁移和播种。我已经设置好使用SQLite,理想情况下想在内存中运行它,但物理文件也没问题。
通过更改Illuminate \ Foundation \ Testing \ DatabaseMigrations,我能够得到我想要的东西; Originial:
public function runDatabaseMigrations()
{
$this->artisan('migrate');
$this->app[Kernel::class]->setArtisan(null);
$this->beforeApplicationDestroyed(function () {
$this->artisan('migrate:rollback');
});
}
我的版本:
public function runDatabaseMigrations()
{
$this->artisan('migrate:refresh');
$this->artisan('db:seed');
$this->app[Kernel::class]->setArtisan(null);
$this->beforeApplicationDestroyed(function () {
$this->artisan('migrate:rollback');
});
}
但是我当然会在未来的更新中放弃这个,所以我需要覆盖我的特性。
我将特征复制到App \ Traits \ DatabaseMigrations.php并更改了命名空间。
<?php
namespace App\Traits;
use Illuminate\Contracts\Console\Kernel;
trait DatabaseMigrations
{
/**
* Define hooks to migrate the database before and after each test.
*
* @return void
*/
public function runDatabaseMigrations()
{
$this->artisan('migrate:refresh');
$this->artisan('db:seed');
$this->app[Kernel::class]->setArtisan(null);
$this->beforeApplicationDestroyed(function () {
$this->artisan('migrate:rollback');
});
}
}
MyTest.php如下:
<?php
namespace Tests\Browser;
use App\User;
use Tests\Browser\Pages\Dashboard;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
//use Illuminate\Foundation\Testing\DatabaseMigrations;
use App\Traits\DatabaseMigrations;
class MyTest extends DuskTestCase
{
use DatabaseMigrations;
但这不起作用。我收到一个错误: PDOException:SQLSTATE [HY000]:常规错误:1没有这样的表:users
因此,迁移不会在没有错误的情况下运行,但结果是我的测试因为没有表而失败。
这是什么工作:
<?php
namespace Tests\Browser;
use App\User;
use Tests\Browser\Pages\Dashboard;
use Illuminate\Contracts\Console\Kernel;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
// use App\Traits\DatabaseMigrations;
class WorkshopRegistrationTest extends DuskTestCase
{
public function runDatabaseMigrations()
{
$this->artisan('migrate:refresh');
$this->artisan('db:seed');
$this->app[Kernel::class]->setArtisan(null);
$this->beforeApplicationDestroyed(function () {
$this->artisan('migrate:rollback');
});
}
use DatabaseMigrations;
但我不想将该方法放入每个Test文件中。有人知道为什么一种方法有效,但另一种方法不起作用吗?在我看来,第一种方法对OOP知之甚少,完全正常。