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

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

图片转代码服务由CSDN问答提供 功能建议

我的功能测试文件

 &lt;?php 
namespace  api \ tests; 
use api \ tests \ FunctionalTester; 
use Yii; 
 
class ExampleCest 
 {
 public function _before(FunctionalTester $ I)
 {
} 
 
 public function _after(FunctionalTester $  I)
 {
} 
 
 //测试
公共函数tryToTest(FunctionalTester $ I)
 {
 $ I-&gt; sendGET('example / test2'); 
 $ I-&gt;  ; seeResponseCodeIs(200);  //未经授权
 $ I-&gt; seeResponseContains('test2');  
} 
} 
   
 
 

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

 <  code> // 1. functional.suite.yml,test run successfully:
 actor:FunctionalTester 
 modules:
 enabled:
  -  REST:
 url:http://api.xxxxx.local/v1/  
取决于:PhpBrowser 
 part:Json 
  -  \ api \ tests \ Helper \ Functional 
 
 
 // 2. functional.suite.yml,测试失败,未在Yii配置文件中设置defaultRoute,但是成功,如果 我设置''defaultRoute'=&gt;  'v1 / example / test2',`:
 actor:FunctionalTester 
 modules:
 enabled:
  -  REST:
 url:v1 / 
取决于:Yii2 
 part:Json 
  -  \ api \  tests \ Helper \ Functional 
   
 
 

失败时的错误消息:

ExampleCest:尝试测试\ n签名:api \ tests \ ExampleCest:tryToTest 测试:tests / functional / ExampleCest.php:tryToTest 场景 - 我发送get“example / test2” [请求] GET v1 / example / test2 \ n [请求标题] [] [yii \ web \ HttpException:404]'yii \ base \ InvalidRouteException:无法解析请求“”。 in /Applications/MAMP/htdocs/gaea/vendor/yiisoft/yii2/base/Module.php:537

nn

我不知道为什么第二个功能 .suite.yml文件很糟糕,因为我可以在浏览器中得到正确的响应 http://localhost/MYPROJECT/api/web/index.php?r = v1 / example / test2

我的环境: \ n

  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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题