phpunit - ZF2单元测试专辑模块返回路由问题

标签 phpunit zend-framework2

我正在尝试 Zf2 专辑模块中的 phpunit。我遇到了一个有关路由的错误。

下面是调试信息。它说“未找到名称为“album”的路由”,但是当我检查相册模块文件夹中的 module.config.php 时,我发现它设置正确,并且在浏览器中重定向到该路由工作正常。

Album\Controller\AlbumControllerTest::testDeleteActionCanBeAccessed
Zend\Mvc\Router\Exception\RuntimeException: Route with name "album" not found
D:\www\zend2\vendor\zendframework\zendframework\library\Zend\Mvc\Router\SimpleRouteStack.php:292
D:\www\zend2\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\Plugin\Url.php:88
D:\www\zend2\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\Plugin\Redirect.php:54
D:\www\zend2\module\Album\src\Album\Controller\AlbumController.php:80
D:\www\zend2\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractActionController.php:87
D:\www\zend2\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php:468
D:\www\zend2\vendor\zendframework\zendframework\library\Zend\EventManager\EventManager.php:208
D:\www\zend2\vendor\zendframework\zendframework\library\Zend\Mvc\Controller\AbstractController.php:108
D:\www\zend2\tests\module\Album\src\Album\Controller\AlbumControllerTest.php:35
C:\wamp\bin\php\php5.4.3\phpunit:46

据我所知,AlbumController.php 第 80 行的问题是

return $this->redirect()->toRoute('album');

但不确定为什么它不起作用。有人遇到过并解决过这样的问题吗?

最佳答案

为了避免重复代码,您可以从模块配置加载路由:

$module = new \YourNameSpace\Module();
$config = $module->getConfig();

$route = \Zend\Mvc\Router\Http\Segment::factory($config['router']['routes']['Home']['options']);

$router = new \Zend\Mvc\Router\SimpleRouteStack();
$router->addRoute('Home', $route);

关于phpunit - ZF2单元测试专辑模块返回路由问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13010051/

相关文章:

zend-framework2 - 使用 Zend 框架 2 未在 JSON 中呈现 404 HTTP 错误

php - 如何更新在 Ubuntu 上不工作的 zend-framework

zend-framework2 - Zf2 NOT IN表达式在Where子句中

PHPUnit模拟父方法

PHPUnit:测试对象数组

php - (PHP) : Testing models with Zend_Test_PHPUnit_DatabaseTestCase

symfony - 如何在单元测试我的 Symfony2 包时显示 PHP 错误?

php - 我应该用 GIT 发送我的 Composer 供应商目录吗

zend-framework2 - Zend Framework 多语言集成步骤

php - zf2 为什么要在模块配置中的可调用数组中添加 Controller