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

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

问题描述

当用户在没有正确的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 Framework进行问题测试异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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