2012-01-04 246 views
2

我試圖用Zend_Test_PHPUnit編寫單元測試我的應用程序,但我總是隻得到Zend框架單元測試

1) IndexControllerTest::testValidation 
Failed asserting last controller used <"error"> was "test" 

我創建了一個測試控制器,但即使在那裏,我不能讓它開始工作。

任何人都可以幫忙嗎?

謝謝!

class TestController extends Zend_Controller_Action 
{ 

    public function indexAction() 
    { 
     print 'test'; 
    } 

} 

引導是:

// Define path to application directory 
defined('APPLICATION_PATH') 
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); 

// Define application environment 
defined('APPLICATION_ENV') 
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing')); 

// Ensure library/ is on include_path 
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'), 
    get_include_path(), 
))); 

require_once '的Zend /裝載機/ Autoloader.php'; Zend_Loader_Autoloader :: getInstance();

phpunit.xml是:

<phpunit bootstrap="./bootstrap.php"> 
    <testsuite name="Application Test Suite"> 
     <directory>./application</directory> 
    </testsuite> 
    <testsuite name="Library Test Suite"> 
     <directory>./library</directory> 
    </testsuite> 

    <filter> 
    <whitelist> 
     <directory suffix=".php">../library/</directory> 
     <directory suffix=".php">../application/</directory> 
     </whitelist> 
    </filter> 
</phpunit> 

測試控制器

class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase 
{ 

    public function setUp() 
    { 
     $this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini'); 
     parent::setUp(); 
    } 

    public function testValidation() 
    { 
     $this->dispatch('/test/'); 
     $this->assertController("test"); 
    } 
} 

回答

0

看起來像你的的TestController錯誤。

您的查看是否可用(/test/index.phtml)?

好的解決方案是檢查拋出的異常(將單元測試包裝在Try/Catch Block中並打印到錯誤日誌中)。

+0

這是缺少的觀點。我只創建了一個JSON-API,這就是爲什麼我不需要視圖。 謝謝! – thesonix 2012-01-05 10:55:47

0

我得到了完全相同的錯誤,但由於完全不同的問題。

一個更普遍的解決方案可能是添加到測試方法

print_r(strip_tags(
     $this->bootstrap 
      ->getBootstrap() 
      ->getResource('layout') 
      ->content 
    )); die; 

這是假設正在使用的Zend_Layout這個......它打印出error.phtml文件的內容,所以至少你可以看到究竟發生了什麼。

你應該看到:

An error occurred 
Application error 

Exception information: 

    Message: {Your Error Message will appear here} 


Stack trace: 
    {A stack trace will follow...} 

隨着之後出現的特定錯誤消息「消息:」再經過一個完整的堆棧跟蹤「堆棧跟蹤」。

希望這至少有助於調試潛在的問題