Zend Framework 2(ZF2) - PHPUnit和$ this-> url() - > toRoute()不起作用

时间:2012-10-18 22:52:30

标签: url frameworks zend-framework2 phpunit

我正在阅读关于ZF 2(http://framework.zend.com/manual/2.0/en/user-guide/routing-and-controllers.html)并且我已经编写了一个控制器Test,但我已经有了我的控制器中有一个编码的Action,如果没有输入ID,它会将用户引导到indexAction,使用PHPUnit执行测试的工作取决于我如何进行重定向示例:

// Does not Work
return $ this-> redirect () -> toRoute ('my_route', array ('action' => 'index'));

返回以下错误消息:

  

TopAcl \ Controller \ MY_CONTROLLER_ControllerTest :: testEditarActionCanBeAcessed Zend \ Mvc \ Router \ Exception \ RuntimeException:找不到名称为“my_route”的路线

// Works correctly
return $ this-> redirect () -> toUrl ('/ resource');

我在网上没有发现任何关于它的内容,有什么解释吗?我还需要一些加载文件吗?

// Follow the setUp of my ControllerTest:
protected function setUp ()
{
    $ bootstrap = \Zend\Mvc\Application :: init (include 'config/application.config.php');
    $ this-> controller = new ResourceController ();
    $ this-> request = new Request ();
    $ this-> routeMatch = new RouteMatch (array ('controller' => 'index'));
    $ this-> event = $ bootstrap-> getMvcEvent ();
    $ this-> event-> setRouteMatch ($ this-> routeMatch);
    $ this-> controller-> SetEvent ($ this-> event);
    $ this-> controller-> setEventManager ($ bootstrap-> getEventManager ());
    $ this-> controller-> setServiceLocator ($ bootstrap-> getServiceManager ());
}

1 个答案:

答案 0 :(得分:0)

我认为这是问题所在:

  

toRoute('my_route',array('action'=>'index')

我希望在module / Application / config / module.config.php中看到配置的路由,最好将配置保留在代码之外

return array(
'router' => array(
    'routes' => array(
        'my_route' => array(
            'type' => 'Literal',
            .....