ZF2测试:断言响应代码"302"失败,实际状态代码为"500". [英] ZF2 Testing: Failed asserting response code "302", actual status code is "500"

查看:85
本文介绍了ZF2测试:断言响应代码"302"失败,实际状态代码为"500".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为AuthenticationController实施PHPUnit测试.当我测试/logout路线时:

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');
}

我收到以下错误消息:

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是内部服务器错误.我不知道它从哪里来.

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.

有人知道吗?

PS :

public function setUp()
{

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

推荐答案

我也遇到了同样的问题.就我而言,在先前的测试之一中,会话已经开始,因此发送了标头.结果,我有这种副作用.解决方案之一是彼此独立地运行此类测试.或者,最好使用硒等其他测试工具进行此类测试.

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.

这篇关于ZF2测试:断言响应代码"302"失败,实际状态代码为"500".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆