Error when running unit test
I have this part of code in my indexController:
public function init()
{
$this->_translate = $this->getInvokeArg('bootstrap')->getResource('translate');
}
This works all fine and in PRD i don't get any errors
But when i am running my unit tests it generates the following error:
Fatal error: Call to a member function getResource() on a non-object in K:\stage 5\application\controllers\IndexController.php on line 9
This is my testcode:
class IndexControllerTest extends ControllerTestCase
{
public function testCanDispla开发者_如何学JAVAyOurHomepage()
{
//Go to the main page of the webapplication
$this->dispatch('/');
//check if we don't end up in an error controller
$this->assertNotController('error');
$this->assertNotAction('error');
//Ok no error lets check if we are on the home page
$this->assertModule('default');
$this->assertController('index');
$this->assertAction('index');
$this->assertResponseCode(200);
}
}
This was not working in my unit tests because I was only calling the application bootstrap and not then calling run.
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();
To fix the problem I added this to the phpunit boostrap
$appBootstrap = $application->getBootstrap();
$front = $appBootstrap->getResource('FrontController');
$front->setParam('bootstrap', $appBootstrap);
and now $bootstrap = $this->getInvokeArg('bootstrap');
works fine in PHPUnit.
Have you tried calling the bootstrap for translate in your Bootstrap call?
$this->bootstrap("translate")
精彩评论