简体   繁体   English

使用 yii2 中的夹具进行代码接收验收测试

[英]codeception acceptance test with fixtures in yii2

Sorry for maybe silly question, but I cannot find answer through googling.对不起,这个问题可能很愚蠢,但我无法通过谷歌搜索找到答案。

My question is: I created file TaskCest.php under backend\\acceptance, In that file have following declaration我的问题是:我在后端\\接受下创建了文件 TaskCest.php,在该文件中有以下声明

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

I have that fixture class with data in data directory.我在数据目录中有带有数据的夹具类。

But when I run script I get following error:但是当我运行脚本时,出现以下错误:

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

Error is obvious, but I cant understand, in file yii2\\test\\FixtureTrait.php:145 I have function which expects name parameter to be string but object passed automatically [I dont call getFixture].错误很明显,但我无法理解,在文件 yii2\\test\\FixtureTrait.php:145 中,我有一个函数,它期望名称参数为字符串,但对象自动传递[我不调用 getFixture]。 What's problem.什么问题。 Did someone faced the same?有人遇到同样的情况吗?

-vvv output -vvv 输出

Test tests/acceptance/TaskCest.php:getFixture测试测试/验收/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

Codeception recognize trait's methods like tests (it search all public methods of Cest -class include trait's methods and run it). Codeception 像测试一样识别 trait 的方法(它搜索Cest类的所有公共方法,包括 trait 的方法并运行它)。 You should extract trait to another class FixtureLoader , and include it into your Cest file.您应该将 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM