2

我有一个简单的控制器动作

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('Zend\View\Model\ViewModel', $result);
}

它工作得很好。

现在我正在转发请求

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

你如何/应该如何用前锋测试动作方法?

谢谢

4

1 回答 1

0

你试过这样的调度吗?我刚刚尝试在我的一个控制器操作中转发,并且单元测试工作正常。这是我的代码:

use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase

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('Catalogue\Controller\Index');
        $this->assertControllerClass('IndexController');
        $this->assertActionName('index');
        $this->assertMatchedRouteName('logcataloguen');
    }

}
于 2013-04-30T10:56:51.067 回答