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

このようにディスパッチしてみましたか?コントローラー アクションの 1 つ内で転送を試みたところ、単体テストは正常に動作します。これは私のコードです:

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 に答える