使用PHPUnit和Zend Framework测试异常时出现问题


Problem testing exceptions with PHPUnit and Zend Framework

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

我现在正试图用PHPUnit测试这种行为。这是我的测试:

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

当我运行测试时,我收到一条消息,说它失败了,"预期异常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');
}

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

好吧,也许它只是因为没有抛出异常而失败?

您是否尝试在没有"@expectedException Zend_Exception"的情况下运行测试?有什么例外吗?