ZF2 Testing: Failed asserting response code “302”, actual status code is “500”

无人久伴 提交于 2019-12-12 18:25:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!