2013-02-26 48 views
1

我已經構建了ZF2 "Getting Started" application,並試圖將ZF2 UnitTesting tutorial中的測試應用到它。現在我有這個簡單的測試一個麻煩:Zend Framework 2問題UnitTesting教程:狀態碼500

[項目] /module/Album/test/AlbumTest/Controller/AlbumControllerTest.php

<?php 
namespace AlbumTest\Controller; 
... 
class AlbumControllerTest extends AbstractHttpControllerTestCase 
{ 
    ... 
    public function testIndexActionCanBeAccessed() 
    { 
     $this->dispatch('/album'); 
     $this->assertResponseStatusCode(200); 

     $this->assertModuleName('Album'); 
     $this->assertControllerName('Album\Controller\Album'); 
     $this->assertControllerClass('AlbumController'); 
     $this->assertMatchedRouteName('album'); 
    } 
} 

命令行:在/ var/WWW /沙/ zf2sandbox /模塊/專輯/測試/

# phpunit 
PHPUnit 3.7.13 by Sebastian Bergmann. 

Configuration read from /var/www/sandbox/zf2sandbox/module/Album/test/phpunit.xml 

F.... 

Time: 0 seconds, Memory: 10.00Mb 

There was 1 failure: 

1) AlbumTest\Controller\AlbumControllerTest::testIndexActionCanBeAccessed 
Failed asserting response code "200", actual status code is "500" 

/var/www/sandbox/zf2sandbox/vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:373 
/var/www/sandbox/zf2sandbox/module/Album/test/AlbumTest/Controller/AlbumControllerTest.php:49 

FAILURES! 
Tests: 5, Assertions: 11, Failures: 1. 

因此,assertResponseStatusCode(200)失敗,因爲狀態代碼是500爲什麼?

當我在瀏覽器(http://zf2sandbox.sandbox.loc/album)稱這條道路,它的工作原理:

enter image description here

THX

回答

3

答案只是在你跟着教程中的下一個標題。 A failing test case.

所以根據它,你需要配置服務管理器。

$serviceManager = $this->getApplicationServiceLocator(); 
$serviceManager->setAllowOverride(true); 
$serviceManager->setService('Album\Model\AlbumTable', $albumTableMock); 

此外,如果在鏈接中提到單元測試中出現適當的錯誤,那麼添加此代碼非常有用。

protected $traceError = true; 
+0

好吧,但我怎麼可能工作和返回狀態302沒有嘲笑?奇怪的行爲是在本地主機上它可以工作,但是當我在主機上運行相同的代碼時,我看到狀態500.可能是什麼原因? – 2015-06-24 06:08:01