2017-06-21 163 views
0

我正在为使用Dusk的Laravel 5.4应用程序开发一套浏览器测试套件。其中一个测试检查是否创建了一个对象,并且由于这是一个浏览器测试,当点击提交按钮时,该值被保存到数据库。显然,我不希望我的数据库变得混乱的测试数据,也有一个问题,因为对象的名称必须是唯一的,因此测试将在第一次运行后失败。我决定解决这个问题的最佳解决方案是实施一个测试数据库,在每个测试周期结束时清除。不过,我在设置时遇到了一些问题。设置内存SQLite数据库在Laravel 5.4中测试

目前,我的代码不会抛出任何错误,但phpunit不会使用测试数据库。我在研究如何实现数据库时安装了代码隐藏,但我不确定如何处理它。下面是代码我目前有:

.ENV

DB_CONNECTION=mysql 

.env.testing

APP_ENV=testing 
APP_DEBUG=true 
APP_KEY=this_has_a_real_key 
APP_URL=http://localhost:8000 

DB_CONNECTION=sqlite_testing 

database.php中

'default' => env('DB_CONNECTION', 'mysql'), 
'connections' => [ 

     ... 

     'sqlite_testing' => [ 
      'driver' => 'sqlite', 
      'database' => ':memory:', 
      'prefix' => '', 
     ], 

     ... 

phpunit.xml

<env name="DB_CONNECTION" value="sqlite_testing" /> 

DuskTestCase.php

abstract class DuskTestCase extends BaseTestCase 
{ 
    /** 
    * Creates the application. 
    * 
    * @return \Illuminate\Foundation\Application 
    */ 
    public function createApplication() 
    { 
     putenv('DB_CONNECTION=sqlite_testing'); 

     $app = require __DIR__ . '/../bootstrap/app.php'; 

     $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap(); 

     return $app; 
    } 

    public function setUp() 
    { 
     parent::setUp(); 
     //Artisan::call('migrate:refresh'); 
     Artisan::call('migrate'); 
     Artisan::call('db:seed'); 
    } 

     /** 
    * Prepare for Dusk test execution. 
    * 
    * @beforeClass 
    * @return void 
    */ 
    public static function prepare() 
    { 
     static::startChromeDriver(); 
    } 

    /** 
    * Create the RemoteWebDriver instance. 
    * 
    * @return \Facebook\WebDriver\Remote\RemoteWebDriver 
    */ 
    protected function driver() 
    { 
     return RemoteWebDriver::create(
      'http://localhost:9515', DesiredCapabilities::chrome() 
     ); 
    } 


    public function tearDown() 
    { 
     Artisan::call('migrate:reset'); 
     parent::tearDown(); 
    } 

} 

DuskTestCase和.env.testing示于它们的全部,其他有相关的部分。我如何修改这段代码让phpunit识别并使用我的sqlite_testing数据库?

+0

你不能用':内存:'因为它在一个单独的进程中运行,所以你必须使用一个实际的sqlite.database文件。 https://stackoverflow.com/a/43479233/4233593 –

回答

2

这是因为你的配置被缓存了。

清除你的配置缓存,PHP的工匠配置:明确

现在的PHPUnit将读取XML文件。

决不缓存你的配置在开发环境;)