我正在使用Dusk为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数据库?
最佳答案
这是因为您的配置已缓存.
使用php artisan config清除配置缓存:清除
现在phpunit将读取xml文件.
切勿在开发环境中缓存配置;)
相关文章
- 使用phpunit.xml,.env.dusk.local和sqlite内存数据库在Dusk中设置Laravel 5.4
- php - 将SQLite设置为Laravel 5.1中的单元测试数据库
- 在内存中的SQLite数据库中使用NHibernate进行测试时出现随机错误
- 使用PHPUnit和Mockery进行Laravel测试 - 在Controller测试中设置依赖性
- php - Laravel 5.4测试路由受$request-> ajax()保护,如何进行测试ajax请求?
- php - 如何在Laravel中进行单元测试期间获取视图数据
- c# - 复制内存中的SQLite数据库,以加快单元测试
- 在MySQL中设置大型数据库,以便在R中进行分析