提问者:小点点

功能测试在Yii2与代码欺骗得到404错误


我的功能测试文件

<?php
namespace api\tests;
use api\tests\FunctionalTester;
use Yii;

class ExampleCest
{
    public function _before(FunctionalTester $I)
    {
    }

    public function _after(FunctionalTester $I)
    {
    }

    // tests
    public function tryToTest(FunctionalTester $I)
    {
        $I->sendGET('example/test2');
        $I->seeResponseCodeIs(200); // Unauthorized
        $I->seeResponseContains('test2');    
    }
}

我尝试了2个不同的functional.suite.yml文件来运行测试:

// 1. functional.suite.yml, test run successfully:
    actor: FunctionalTester
    modules:
        enabled:
            - REST:
                url: http://api.xxxxx.local/v1/
                depends: PhpBrowser
                part: Json    
            - \api\tests\Helper\Functional


// 2. functional.suite.yml, test failed without setting defaultRoute in Yii configuration file, but succeed if I set `'defaultRoute' => 'v1/example/test2',`:
    actor: FunctionalTester
    modules:
        enabled:
            - REST:
                url: v1/
                depends: Yii2
                part: Json    
            - \api\tests\Helper\Functional        

失败时显示错误消息:

示例Cest:尝试测试签名:api\测试\示例Cest: tryToTest测试:测试/功能/示例Cest.php: tryToTest场景-我发送get"例如/测试2"[请求]GET v1/例如/测试2[请求头][][yii\web\HttpExc0019:404]'yii\base\InvalidRouteExc0019:无法解决请求""。在 /Applications/MAMP/htdocs/gaea/vendor/yiisoft/yii2/base/Module.php:537

我不知道为什么第二个功能。一套yml文件失败了,因为我可以在浏览器中从http://localhost/MYPROJECT/api/web/index.php?r=v1/example/test2.

我的环境:

Codeception: v2.3.8
Yii: v2.0.14
PHP: v7.0.26

共1个答案

匿名用户

通过将url:v1/更改为url:/v1或将其删除,我解决了这个问题。