【问题标题】:Test Database and Codeception测试数据库和代码接收
【发布时间】:2016-09-18 07:09:03
【问题描述】:

我正在使用 Laravel 5 / Codeception。

我正在使用测试数据库。

这是我的配置:

acceptance.suite.yml:

class_name: AcceptanceTester
modules:
enabled:
    - WebDriver
    - \Helper\Acceptance
    - Db
    - Asserts

config:
    WebDriver:
        url: 'http://laravel.dev'
        browser: 'phantomjs'
        window_size: 1024x768
    Db:
        dsn: 'mysql:host=laravel.dev;dbname=kendo_test'
        user: 'homestead'
        password: 'secret'

所以,在这里我将我的数据库定义为我的测试数据库。

然后,在我的 bootstrap.php 中有:

$app->loadEnvironmentFrom('.env.testing');

还有.env.testing:

DB_HOST=127.0.0.1
DB_DATABASE=kendo_test
DB_USERNAME=homestead
DB_PASSWORD=secret

作为测试,我将 kendo_test 更改为 kendo_test2,但它失败了,它正在使用这个 db。

现在,当我执行验收测试时,我的测试失败了,因为在主数据库中插入了行,而不是测试,我不知道为什么......

这是我的测试:

public function it_create_user(\AcceptanceTester $I, $scenario)
{
    App::setLocale('en');
    $user = factory(User::class)->make();

    $I = new SimpleUser($scenario);
    $I->logAsUser();
    $I->dontSee(trans_choice('core.user', 2) . ' </a></li>');
    $I->logout();
    $I = new SuperAdmin($scenario);
    $I->logAsSuperAdmin();

    $I->click('#dropdown-user');
    $I->click(trans_choice('core.user', 2));
    $I->click(trans('core.addModel', ['currentModelName' => trans_choice('core.user', 1)]));
    $I->fillField('name',$user->name );
    $I->fillField('email',$user->email);
    $I->fillField('firstname',$user->firstname);
    $I->fillField('lastname',$user->lastname);
    $I->fillField('password','111111');
    $I->fillField('password_confirmation','111111');
    $I->click(trans('core.save')); // <-- Here is should save it
    $I->seeInCurrentUrl('/users');
    $I->seeInSource(trans('msg.user_create_successful'));
    $I->seeInDatabase('ken_users', ['name' => $user->name]);
}

知道为什么吗???

【问题讨论】:

  • 当您点击$I-&gt;click(trans('core.save')); 时,它将使用您应用中的.env,而不是$app-&gt;loadEnvironmentFrom 中的那个。
  • 没有。它应该加载 bootstrap.php 中调用的 .env.testing

标签: laravel phpunit codeception


【解决方案1】:

当您点击 $I-&gt;click(trans('core.save')); 时,它将使用您应用中的 .env,而不是 $app-&gt;loadEnvironmentFrom 中的那个。

这是因为在运行验收测试时,您正在通过浏览器与您的应用进行交互。

正在运行的测试有自己的实例,以及测试访问的应用程序。

您在此处使用 $app-&gt;loadEnvironmentFrom 的唯一原因是利用 Eloquent,即便如此,它也必须在单独的连接上。

【讨论】:

    猜你喜欢
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多