簡體   English   中英

使用 yii2 中的夾具進行代碼接收驗收測試

[英]codeception acceptance test with fixtures in yii2

對不起,這個問題可能很愚蠢,但我無法通過谷歌搜索找到答案。

我的問題是:我在后端\\接受下創建了文件 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 中,我有一個函數,它期望名稱參數為字符串,但對象自動傳遞[我不調用 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

Codeception 像測試一樣識別 trait 的方法(它搜索Cest類的所有公共方法,包括 trait 的方法並運行它)。 您應該將 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