【问题标题】:Setting up an in memory SQLite database for testing in Laravel 5.4在 Laravel 5.4 中设置用于测试的内存 SQLite 数据库
【发布时间】:2017-11-24 10:32:21
【问题描述】:

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

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

.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

数据库.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 laravel sqlite laravel-5


【解决方案1】:

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

使用 php artisan config:clear 清除配置缓存

现在 phpunit 将读取 xml 文件。

永远不要在开发环境中缓存你的配置;)

【讨论】:

  • 仍然找不到我的桌子
猜你喜欢
  • 2017-06-13
  • 2018-05-13
  • 2015-09-16
  • 2014-09-03
  • 1970-01-01
  • 2021-03-08
  • 2014-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多