代码之家  ›  专栏  ›  技术社区  ›  Tarasovych

Laravel Dusk意外刷新数据库

  •  0
  • Tarasovych  · 技术社区  · 5 年前

    我有什么:

    PHP:7.2版
    宅基地: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);
            }
        }
    }
    

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

    0 回复  |  直到 5 年前
    推荐文章