我有一个简单的控制器动作
class CatalogController extends AbstractActionController {
public function indexAction() {
return new ViewModel();
}
// ...
}
和它的单元测试:
class CatalogControllerTest extends AbstractHttpControllerTestCase
{
public function testIndexActionCanBeAccessed()
{
$this->routeMatch->setParam('action', 'index');
$result = $this->controller->dispatch($this->request);
$response = $this->controller->getResponse();
$this->assertEquals(200, $response->getStatusCode());
$this->assertInstanceOf('ZendViewModelViewModel', $result);
}
It work fine.
现在我正在转发请求
public function indexAction() {
return $this->forward()->dispatch('Catalog/Controller/Catalog', array('action' => 'list-cities'));
}
和在$this->controller->dispatch($this->request);
:
PHP Fatal error: Call to a member function getEventManager() on a non-object in /var/www/path/to/project/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/Plugin/Forward.php on line 147
如何/应该如何测试forward的action方法?
Thx
你试过这样调度吗?我刚刚尝试转发我的一个控制器动作和单元测试工作良好。这是我的代码:
use ZendTestPHPUnitControllerAbstractHttpControllerTestCase
class IndexControllerTest extends AbstractHttpControllerTestCase
{
public function setUp()
{
require APPLICATION_PATH . '/init_autoloader.php';
$testConfig = include APPLICATION_PATH . '/config/test.php';
$this->setApplicationConfig($testConfig);
parent::setUp();
}
public function testFoo()
{
$this->dispatch('/catalogue');
$this->assertResponseStatusCode(200);
$this->assertModuleName('Catalogue');
$this->assertControllerName('CatalogueControllerIndex');
$this->assertControllerClass('IndexController');
$this->assertActionName('index');
$this->assertMatchedRouteName('logcataloguen');
}
}