使用 PHPUnit 和 Zend 框架测试异常的问题 [英] Problem testing exceptions with PHPUnit and Zend Framework

查看:21
本文介绍了使用 PHPUnit 和 Zend 框架测试异常的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When a user accesses /user/validate without the correct post parameters, my Zend application throws a zend exception. (I get the standard "An Error Occurred" message, framed within my layout). This is intentional.

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

I am now trying to test that behaviour with PHPUnit. Here's my test:

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

当我运行测试时,我收到一条消息,说它失败了,预期异常 Zend_Exception".有什么想法吗?

When I run the test I get a message saying it failed, "Expected exception Zend_Exception". Any ideas?

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

I have other tests in the file which are working just fine...

谢谢!

推荐答案

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

The Zend_Controller_Plugin_ErrorHandler plugin handles exceptions for you and the default Error_Controller forces a 500 redirect which may mean the exception you are testing for no longer exists. Try the following unit test and see if it passes:

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

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

If this works then it shows that by the time you are rendering the error view the exception will no longer exist as it is contained in the code before the redirect.

这篇关于使用 PHPUnit 和 Zend 框架测试异常的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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