4

I'm using Yii2 with Codeception. The problem is that Codeception seems to not see the database dump. For simplicity, I consider only LoginCept acceptance tests:

use tests\_pages\LoginPage;

$I = new WebGuy($scenario);
$I->wantTo('ensure that login works');
$loginPage = LoginPage::openBy($I);

$I->see('Login', 'h1');

$I->amGoingTo('try to login with empty credentials');
$loginPage->login('', '');
$I->expectTo('see validations errors');
$I->see('Username cannot be blank.');
$I->see('Password cannot be blank.');
...

So, when I run

./vendor/bin/codecept run tests/acceptance/LoginCept.php

everything runs without errors:

Acceptance Tests (1) ----------------------------------------
Trying to ensure that login works (LoginCept.php)       Ok
-------------------------------------------------------------

But as soon as I add the following line

echo $I->grabFromDatabase('user', 'name', ['id' => 1]);

the test starts to fail:

Acceptance Tests (1) ----------------------------------------
Trying to ensure that login works (LoginCept.php)       Error
-------------------------------------------------------------
Time: 2.11 seconds, Memory: 9.75Mb
There was 1 error:
---------
1) Failed to ensure that login works in LoginCept.php
#1  F:\site\tests\acceptance\LoginCept.php:8  <-- 8 is the number of added line
#2  F:\site\tests\acceptance\LoginCept.php:8
FAILURES!

Below I put the detailed info from config files.

codeception.yml file has the following content:

paths:
    tests: tests
    log: tests/_log
    data: tests/_data
    helpers: tests/_helpers
settings:
    bootstrap: _bootstrap.php
    suite_class: \PHPUnit_Framework_TestSuite
    memory_limit: 1024M
    log: true
    colors: true
modules:
    enabled: [Db]
    config:
        Db:
            dsn: 'mysql:host=localhost;dbname=site_test' 
            user: 'daemon'
            password: 'HsqyJSAbZC3q3KJa'
            dump: 'tests/_data/dump.sql'
            populate: true
            cleanup: true

The print out of \Yii::$app->db gives:

yii\db\Connection Object
            (
                [dsn] => mysql:host=localhost;dbname=site_test
                [username] => daemon
                [password] => HsqyJSAbZC3q3KJa
                [attributes] =>
                [pdo] =>
                [enableSchemaCache] =>
                [schemaCacheDuration] => 3600
                ...
           )

With the credentials above, I can log in manually to my database (in phpmyadmin).

The database dump is located in F:\site\tests\_data\dump.sql. I've created it from my working database called site and made some modification inside it:

CREATE DATABASE  IF NOT EXISTS `site_test`  DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
USE `site_test`;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` smallint(5),
  `name` varchar(50)
);

INSERT INTO `user` VALUES (1,'admin');

Codeception build command everytime I run it, gives:

$ ./vendor/bin/codecept build
Building Guy classes for suites: acceptance, functional, unit
WebGuy includes modules: WebHelper, PhpBrowser
WebGuy.php generated successfully. 48 methods added
TestGuy includes modules: Filesystem, TestHelper, Yii2
TestGuy.php generated successfully. 53 methods added
CodeGuy includes modules: CodeHelper
CodeGuy.php generated successfully. 1 methods added
4

1 回答 1

7

确保为您的套装配置启用了 Db 模块。

然后你需要codecept build在使用 Db 模块的方法之前运行。

通常,您应该在每次启用或禁用测试套件配置中的模块时运行它。

于 2014-03-29T17:36:49.250 回答