问题
I am implementing PHPUnit tests for an AuthenticationController
. When I test the /logout
route:
public function testLogoutActionCanBeAccessed()
{
$this->dispatch('/logout');
$this->assertResponseStatusCode(302);
$this->assertModuleName('Main');
$this->assertControllerName('Main\Controller\Authentication');
$this->assertControllerClass('AuthenticationController');
$this->assertMatchedRouteName('logout');
}
I get the following error message:
There was 1 failure:
1) MainTest\Controller\AuthenticationControllerTest::testLogoutActionCanBeAccessed
Failed asserting response code "302", actual status code is "500"
The logout code is the following:
public function logoutAction()
{
$this->loginLogoutService->logout();
return $this->redirect()->toRoute('home');
}
and
public function logout() {
$this->authservice->getStorage()->forgetMe();
$this->authservice->clearIdentity();
}
and
$this->authservice = new AuthenticationService();
When I debug my app step-by-step, the $actionResponse
status code is 302 and the application works fine. 500 is internal server error. I have no idea where it is coming from.
Anyone has some idea?
P.S.:
public function setUp()
{
$this->setApplicationConfig(
include '/../../../../../config/application.config.php'
);
parent::setUp();
}
回答1:
I had same problem too. In my case in one of previous tests session was already started and consequently headers was sent. As a result I've got such side effect. One of solutions is run such tests separately from each other. Or maybe better use another testing tool like selenium for test like this.
来源:https://stackoverflow.com/questions/32394839/zf2-testing-failed-asserting-response-code-302-actual-status-code-is-500