设置内存 SQLite 数据库以在 Laravel 5.4 中进行测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44677477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:11:01  来源:igfitidea点击:

Setting up an in memory SQLite database for testing in Laravel 5.4

phplaravelsqlitelaravel-5

提问by Kai D

I am working on developing a suite of browser tests for a Laravel 5.4 application using Dusk. One of the tests checks whether an object is created and since this is a browser test the value is saved to the database when the submit button is clicked. Obviously I don't want my database to become cluttered with test data and there is also an issue since the name of the object has to be unique and therefore the test will fail after the first run. I decided the best solution to this problem was to implement a testing database which is cleared at the end of each test cycle. However I am having some problems setting it up.

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

Currently, my code does not throw any errors but phpunit just will not use the testing database. I installed codeception when I was researching how to implement the database but I'm not sure what to do with it. Here is the code I currently have:

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

.env

.env

DB_CONNECTION=mysql

.env.testing

.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

数据库.php

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

        ...

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

        ...

phpunit.xml

phpunit.xml

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

DuskTestCase.php

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 and .env.testing are shown in their entirety, the others have the relevant portions. How can I modify this code to make phpunit recognise and use my sqlite_testing database?

DuskTestCase 和 .env.testing 是完整显示的,其他有相关部分。如何修改此代码以使 phpunit 识别并使用我的 sqlite_testing 数据库?

回答by Damosse31

It's because your config is cached.

这是因为您的配置已被缓存。

Clear your config cache with php artisan config:clear

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

Now phpunit will read the xml file.

现在 phpunit 将读取 xml 文件。

Never cache your config in dev environment ;)

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