2015-09-04 95 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

我也有同样的问题。在我之前的测试中,会话已经开始,因此发送了头文件。结果我有这样的副作用。其中一种解决方案是将这些测试彼此分开进行。或者,也许最好使用像硒这样的测试工具进行测试。