dswqz24846 2018-04-16 09:44
浏览 404
已采纳

Yii2中的功能测试与代码获得404错误

My functional test file

<?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');    
    }
}

I tried 2 different functional.suite.yml files to run the test:

// 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        

The error message when failed:

ExampleCest: Try to test Signature: api\tests\ExampleCest:tryToTest Test: tests/functional/ExampleCest.php:tryToTest Scenario -- I send get "example/test2" [Request] GET v1/example/test2 [Request Headers] [] [yii\web\HttpException:404] 'yii\base\InvalidRouteException: Unable to resolve the request "". in /Applications/MAMP/htdocs/gaea/vendor/yiisoft/yii2/base/Module.php:537

I have no idea why the 2nd functional.suite.yml file faied, since I can get right response in the browser from http://localhost/MYPROJECT/api/web/index.php?r=v1/example/test2.

My environment:

Codeception: v2.3.8
Yii: v2.0.14
PHP: v7.0.26
  • 写回答

1条回答 默认 最新

  • douzhi4991 2018-04-17 03:56
    关注

    I solved this problem by changing url: v1/ to url: /v1 or removing it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!