ZF2 Testing: Failed asserting response code “302”,

2019-07-11 06:36发布

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条回答
Root(大扎)
2楼-- · 2019-07-11 07:27

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.

查看更多
登录 后发表回答