【问题标题】:ZF2 Testing: Failed asserting response code "302", actual status code is "500"ZF2 测试:断言响应代码“302”失败,实际状态代码为“500”
【发布时间】:2015-11-30 10:08:07
【问题描述】:

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

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

有人有什么想法吗?

附注

public function setUp()
{

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

【问题讨论】:

  • 尝试在测试类的顶部添加protected $traceError = true;。它应该为您提供更好的错误响应。
  • 我刚做了,但错误信息完全一样。它没有提供更多信息。
  • 您是否在测试设置中包含了应用程序配置?如果您删除状态代码的断言,其他的是否有效?
  • 是的,请参阅我的问题更新。其他路线测试有效。只有这个失败了。
  • 什么更新?你能告诉我你的setUp()函数吗?

标签: php zend-framework2 phpunit


【解决方案1】:

我也有同样的问题。就我而言,在之前的一项测试中,会话已经开始,因此发送了标题。结果我得到了这样的副作用。一种解决方案是彼此分开运行此类测试。或者也许更好地使用像 selenium 这样的其他测试工具来进行这样的测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    相关资源
    最近更新 更多