Zend Framework 中的 UnitTest 错误控制器
我喜欢 100% 代码覆盖率,但我不知道如何在 Zend Framework 中测试 ErrorController。
测试404Action和errorAction是没有问题的:
public function testDispatchErrorAction()
{
$this->dispatch('/error/error');
$this->assertResponseCode(200);
$this->assertController('error');
$this->assertAction('error');
}
public function testDispatch404()
{
$this->dispatch('/error/errorxxxxx');
$this->assertResponseCode(404);
$this->assertController('error');
$this->assertAction('error');
}
但是如何测试应用程序错误(500)呢? 也许我需要这样的东西?
public function testDispatch500()
{
throw new Exception('test');
$this->dispatch('/error/error');
$this->assertResponseCode(500);
$this->assertController('error');
$this->assertAction('error');
}
Iam a fan of 100% code coverage, but i have no idea how to test the ErrorController in Zend Framework.
It is no problem to test the 404Action and the errorAction:
public function testDispatchErrorAction()
{
$this->dispatch('/error/error');
$this->assertResponseCode(200);
$this->assertController('error');
$this->assertAction('error');
}
public function testDispatch404()
{
$this->dispatch('/error/errorxxxxx');
$this->assertResponseCode(404);
$this->assertController('error');
$this->assertAction('error');
}
But how to test for an application error (500)?
maybe i need something like this?
public function testDispatch500()
{
throw new Exception('test');
$this->dispatch('/error/error');
$this->assertResponseCode(500);
$this->assertController('error');
$this->assertAction('error');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个老问题,但我今天一直在努力解决这个问题,在其他地方找不到好的答案,所以我将继续发布我为解决这个问题所做的事情。答案其实很简单。
将您的调度指向将导致抛出异常的操作。
当向 JSON 端点发出 get 请求时,我的应用程序会抛出错误,因此我使用其中之一来测试这一点。
或者,如果您不介意仅用于测试的操作,则可以创建一个仅引发错误的操作并在单元测试中指向该操作。
那么你的测试将如下所示:
This is an old question but I was struggling with this today and couldn't find a good answer anywhere else so I'll go ahead and post what I did to solve this problem. The answer is actually really simple.
Point your dispatch to an action that will result in a thrown exception.
My application throws an error when a get request is made to a JSON end point, so I used one of those to test this.
Alternatively, if you don't mind having an action just for testing, you could just create an action that only throws an error and point to that action in your unit test.
Then your test would look like this:
好吧,我对这个主题不太熟悉,但我会使用自定义 ErrorHandler 插件来操纵此行为(扩展原始版本,并假装抛出异常)。也许可以只注册一次测试。
Well, I'm not very familiar with this subject, but I'd manipulate this behavior with a custom ErrorHandler plugin (extend the original, and pretend that an exception was thrown). Maybe it's possible to register it only for one test.