php - 在 Yii2 中使用 codeception 进行功能测试时出现 404 错误

标签 php testing yii2 codeception

我的功能测试文件

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

失败时的错误信息:

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

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

我的环境:

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

最佳答案

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

关于php - 在 Yii2 中使用 codeception 进行功能测试时出现 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49854215/

相关文章:

php - 如何在yii2中实现单一搜索表单

php - Symfony2 在每个循环中更改元素样式

php - 我在为图像创建单独的表时遇到问题

postgresql - Yii2:如何使用 PostgreSQL 的函数?

java - 这有什么问题吗?我找不到符号

ios - 在为 IOS 发布之前如何查看我的应用程序页面?

javascript - 如何在视差 slider 中添加视频?

php - 尝试使用 Kohana 分页模块的问题

javascript - 将所有 JS 和 CSS 保证为内联?

python - 编写仅测试函数是否被调用的单元测试有意义吗?