gpt4 book ai didi

php - 使用 PHPUnit 和 Zend Framework 测试异常问题

转载 作者:可可西里 更新时间:2023-11-01 13:20:06 25 4
gpt4 key购买 nike

当用户在没有正确的 post 参数的情况下访问/user/validate 时,我的 Zend 应用程序会抛出一个 zend 异常。 (我收到标准的“发生错误”消息,在我的布局中加框)。这是故意的。

我现在正尝试使用 PHPUnit 测试该行为。这是我的测试:

/**
* @expectedException Zend_Exception
*/
public function testEmptyUserValidationParametersCauseException() {
$this->dispatch('/user/validate');
}

当我运行测试时,我收到一条消息说它失败了,“Expected exception Zend_Exception”。有什么想法吗?

我在文件中还有其他测试工作正常...

谢谢!

最佳答案

Zend_Controller_Plugin_ErrorHandler 插件为您处理异常,默认的 Error_Controller 强制 500 重定向,这可能意味着您正在测试的异常不再存在。尝试以下单元测试,看看它是否通过:

public function testUnknownUserRedirectsToErrorPage()
{
$this->dispatch('/user/validate');
$this->assertController('error');
$this->assertAction('error');
$this->assertResponseCode('500');
}

如果这有效,则表明在您呈现错误 View 时异常将不再存在,因为它包含在重定向之前的代码中。

关于php - 使用 PHPUnit 和 Zend Framework 测试异常问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6545656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com