2011-01-23 167 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中正常工作。