在yii2中使用fixture的代码验收测试

时间:2017-01-12 10:10:23

标签: php yii2-advanced-app codeception acceptance-testing

对不起可能是愚蠢的问题,但我无法通过谷歌搜索找到答案。

我的问题是: 我在后端\接受下创建了文件TaskCest.php,在该文件中有以下声明

use yii\test\FixtureTrait;
    public function fixtures() {
      return ['tasks' => TasksFixture::className()];
    }

我在数据目录中有数据的夹具类。

但是当我运行脚本时,我得到以下错误:

[yii\base\ErrorException] ltrim() expects parameter 1 to be string, object given

错误是显而易见的,但我不明白,在文件yii2 \ test \ FixtureTrait.php:145我有函数,它期望name参数是字符串但对象自动传递[我不调用getFixture]。 什么问题。有人面对同样的事吗?

-vvv输出

测试测试/验收/ TaskCest.php:getFixture

[yii\base\ErrorException] ltrim() expects parameter 1 to be string, object given  

/home/nginx/www/planning-back/vendor/codeception/codeception/src/Codeception/Lib/Di.php:123
/home/nginx/www/planning-back/vendor/codeception/codeception/src/Codeception/Lib/Di.php:123
/home/nginx/www/planning-back/vendor/codeception/codeception/src/Codeception/Test/Cest.php:136
/home/nginx/www/planning-back/vendor/codeception/codeception/src/Codeception/Test/Cest.php:148
/home/nginx/www/planning-back/vendor/codeception/codeception/src/Codeception/Test/Cest.php:82
/home/nginx/www/planning-back/vendor/codeception/codeception/src/Codeception/Test/Test.php:90
/home/nginx/www/planning-back/vendor/phpunit/phpunit/src/Framework/TestSuite.php:728
/home/nginx/www/planning-back/vendor/codeception/codeception/src/Codeception/PHPUnit/Runner.php:98
/home/nginx/www/planning-back/vendor/codeception/codeception/src/Codeception/SuiteManager.php:154
/home/velaro/.config/composer/vendor/codeception/codeception/src/Codeception/Codecept.php:183
/home/velaro/.config/composer/vendor/codeception/codeception/src/Codeception/Codecept.php:152
/home/velaro/.config/composer/vendor/codeception/codeception/src/Codeception/Command/Run.php:282
/home/velaro/.config/composer/vendor/symfony/console/Command/Command.php:255
/home/velaro/.config/composer/vendor/symfony/console/Application.php:829
/home/velaro/.config/composer/vendor/symfony/console/Application.php:191
/home/velaro/.config/composer/vendor/symfony/console/Application.php:122
/home/velaro/.config/composer/vendor/codeception/codeception/src/Codeception/Application.php:103
/home/velaro/.config/composer/vendor/codeception/codeception/codecept:34

1 个答案:

答案 0 :(得分:1)

Codeception识别trait的方法,比如测试(它搜索Cest的所有公共方法 - 类包括trait的方法并运行它)。 您应该将特征提取到另一个类FixtureLoader,并将其包含在Cest文件中。

class FixtureLoader
{
    use \yii\test\FixtureTrait;

    public $fixtures;

    public function fixtures()
    {
        return $this->fixtures;
    }
}

abstract class ApiCest
{
    /**
     * @var FixtureLoader
     */
    protected $fixtureLoader;

    public function __construct()
    {
        $this->fixtureLoader = new FixtureLoader();
    }

    protected function fixtures()
    {
        return [];
    }

    public function _before(\FunctionalTester $I)
    {
        $this->fixtureLoader->fixtures = $this->fixtures();
        $this->fixtureLoader->loadFixtures();
    }

    public function _after(\FunctionalTester $I)
    {
        $this->fixtureLoader->unloadFixtures();
    }
}


class UserCest extends ApiCest
{
    protected function fixtures()
    {
        return [
            'users' => UserFixture::className(),
        ];
    }

    public function testSomething(\FunctionalTester $I)
    {
        $I->sendGET('/user');
        $I->seeResponseCodeIs(200);
    }
}
相关问题