如何正确更改测试环境变量-Codeception和Laravel 5.1 [英] How to properly change testing environment vars - Codeception and Laravel 5.1

查看:118
本文介绍了如何正确更改测试环境变量-Codeception和Laravel 5.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图将sqlite db用于测试目的,因为我不想触碰我的实际数据库:

I am trying to use a sqlite db for testing purposes, as I don't want to touch my actual database:

在我的unit.suite.yml文件中:

In my unit.suite.yml file:

class_name: UnitTester
modules:
    enabled: [Asserts, UnitHelper]
    config:
        Db:
            dsn: 'sqlite:app/tests/_data/testdb.sqlite'
            user: ''
            password: ''
            dump: app/tests/_data/dump.sql

在我的TestCase.php文件中,我有一个函数可以重置,迁移和播种数据库以进行测试:

In my TestCase.php file I have a function which resets and migrates and seeds the db for testing:

        Artisan::call('migrate:reset');
        Artisan::call('migrate');
        Artisan::call('db:seed');

我还将sqlite设置添加到app/config/testing/database.php:

I also added the sqlite settings to app/config/testing/database.php:

<?php
// Codeception testing environment uses SQLITE

return [

    'default' => 'sqlite',

    'connections' => [
        'sqlite' => [
            'driver' => 'sqlite',
            'database' => 'app/tests/_data/testdb.sqlite',
            'prefix' => ''
        ],
    ]
];

我可以确认正在读取database.php文件,但是仍然有一些东西阻止sqlite的使用,并且我仍然最终使用了我的REAL数据库(MySQL).如何强制Laravel/Codeception使用我的测试数据库?

I can verify that the database.php file is being read but something still prevents sqlite from being used, and I still end up hosing my REAL database (MySQL). How do I force Laravel/Codeception to use my test db?

推荐答案

所以我受够了,并决定做这件事:

So I got fed up and decided to do this hacky thing:

在.env中,我添加了以下内容:

In .env I added this:

DB_DEFAULT=mysql

然后我将sqlite_testing配置添加到database.php文件中,并如下更改默认设置:

then I added my sqlite_testing config to the database.php file and I altered the default setting like so:

'default' => env('DB_DEFAULT', 'mysql'),

最后,我将其添加到TestCase.php的开头:

Lastly, I added this to the beginning of my TestCase.php:

    putenv('DB_DEFAULT=sqlite_testing');

我坚持这样做,因为它确实有效,但是任何评论/建议都值得赞赏.我在laracasts网站上找到了此解决方案:

I'm sticking with this because it actually works, but any comment/suggestions are appreciated. I found this solution on laracasts site:

https://laracasts.com/discuss/channels/testing/how-to-specify-a-testing-database-in-laravel-5

这篇关于如何正确更改测试环境变量-Codeception和Laravel 5.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆