【问题标题】:codeception acceptance test with fixtures in yii2在 yii2 中使用夹具进行代码接收验收测试
【发布时间】:2017-01-12 10:10:22
【问题描述】:

抱歉,这个问题可能很愚蠢,但我无法通过谷歌搜索找到答案。

我的问题是: 我在 backend\acceptance 下创建了文件 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

【问题讨论】:

  • 可以检查或粘贴错误堆栈吗?我找不到你从哪里得到这个。
  • 使用--debug 为堆栈运行代码接收。
  • 就像我用红色打印一样,没有堆栈,即使我使用调试选项运行
  • 嗯...也许检查-vvv标志。

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


【解决方案1】:

Codeception 识别 trait 的方法,如测试(它搜索 Cest-class 的所有公共方法,包括 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);
    }
}

【讨论】:

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