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

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.

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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵