首页 > 解决方案 > Laravel Dusk 意外的数据库刷新

问题描述

我有什么:
Laravel:5.7.28
PHP:7.2
Homestead:8.0.2

测试类:

class ExampleTest extends DuskTestCase
{
    use DatabaseMigrations;

    public function testExample()
    {
        $user = factory(User::class)->create(/**/);

        $faker = Factory::create();

        $this->browse(function (Browser $browser) use ($user) {
            $browser->loginAs($user)
                ->visit(/**/)
                ->type(/**/)
                ->press(/**/)
                ->assertRouteIs(/**/);
        });
    }

    public function testExample2() //identical to testExample
      {
        //...

        $this->browse(function (Browser $browser) use ($user) {
            //...
        }); // <-- Base table or view not found: 1146 Table 'testing.users' doesn't exist
    }
}

问题:为什么数据库会刷新(?)$this->browse()

我在每次测试之前运行了一些种子,但它们与users表没有直接关系。

abstract class DuskTestCase extends BaseTestCase
{
    use CreatesApplication;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed(\RolesSeeder::class);
    }
}
trait CreatesApplication
{
    public function createApplication()
    {
        //...

        $this->clearCache();

        return $app;
    }

    protected function clearCache()
    {
        $commands = ['clear-compiled', 'cache:clear', 'view:clear', 'config:clear', 'route:clear'];

        foreach ($commands as $command) {
            Artisan::call($command);
        }
    }
}

每个测试类都有一个测试方法,一切正常。

标签: laravel-dusk

解决方案


推荐阅读