2011-01-23 140 views
0

时,我有这部分代码在我indexController的:错误运行单元测试

public function init() 
{ 
    $this->_translate = $this->getInvokeArg('bootstrap')->getResource('translate'); 
} 

这工作都很好,在珠三角我没有得到任何错误

但是,当我运行我的单位测试中,它会生成以下错误:

Fatal error: Call to a member function getResource() on a non-object in K:\stage 5\application\controllers\IndexController.php on line 9

这是我testcode:

class IndexControllerTest extends ControllerTestCase 
{ 
    public function testCanDisplayOurHomepage() 
    { 
     //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); 


    } 
} 
+0

请让我知道类的getResource和类$ this-> getInvokeArg('bootstrap')。 – Gaurav 2011-01-23 17:50:36

回答

1

您是否试过在Bootstrap调用中调用bootstrap进行翻译?

$this->bootstrap("translate") 
0

检查此answer

看起来Zend_Controller_Action->getInvokeArg('bootstrap')在单元测试环境中不起作用。

3

这不是在我的单元测试工作,因为我只是调用应用程序引导,而不是然后调用运行。

/** 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(); 

要解决我已将此添加到PHPUnit的自举

$appBootstrap = $application->getBootstrap(); 
$front = $appBootstrap->getResource('FrontController'); 
$front->setParam('bootstrap', $appBootstrap); 

问题,现在$bootstrap = $this->getInvokeArg('bootstrap'); PHPUnit中正常工作。