2015-09-04 54 views
2

我正在爲AuthenticationController實施PHPUnit測試。當我測試的/logout路線:ZF2測試:斷言響應代碼「302」失敗,實際狀態代碼爲「500」

public function testLogoutActionCanBeAccessed() 
{ 
    $this->dispatch('/logout'); 
    $this->assertResponseStatusCode(302); 

    $this->assertModuleName('Main'); 
    $this->assertControllerName('Main\Controller\Authentication'); 
    $this->assertControllerClass('AuthenticationController'); 
    $this->assertMatchedRouteName('logout'); 
} 

我收到以下錯誤信息:

There was 1 failure: 

1) MainTest\Controller\AuthenticationControllerTest::testLogoutActionCanBeAccessed 
Failed asserting response code "302", actual status code is "500" 

註銷代碼如下:

public function logoutAction() 
{ 
    $this->loginLogoutService->logout(); 

    return $this->redirect()->toRoute('home'); 
} 

public function logout() { 
    $this->authservice->getStorage()->forgetMe(); 
    $this->authservice->clearIdentity(); 
} 

$this->authservice = new AuthenticationService(); 

當我一步一步調試我的應用程序時,$actionResponse狀態碼是302,應用程序工作正常。 500是內部服務器錯誤。我不知道它來自哪裏。

任何人有一些想法?

P.S.

public function setUp() 
{ 

    $this->setApplicationConfig(
     include '/../../../../../config/application.config.php' 
    ); 
    parent::setUp(); 
} 
+1

嘗試在測試類的頂部添加'protected $ traceError = true;'。它應該爲您提供更好的錯誤響應。 – Ankh

+0

我剛剛做了,但是錯誤信息完全一樣。它不提供更多信息。 – JVerstry

+0

您是否在測試設置中包含了應用程序配置?如果你刪除狀態碼的斷言,其他人是否工作? – Ankh

回答

1

我也有同樣的問題。在我之前的測試中,會話已經開始,因此發送了頭文件。結果我有這樣的副作用。其中一種解決方案是將這些測試彼此分開進行。或者,也許最好使用像硒這樣的測試工具進行測試。