PHPUnit Zend Framework 控制器测试 - 断言最后使用的控制器失败<“错误”> - 显示错误

发布于 2024-11-07 14:04:14 字数 800 浏览 7 评论 0 原文

我们目前正在围绕 zend 框架控制器进行单元测试。

(我已经稍微抽象了这个代码示例,但想法是相同的......)

我们很高兴地设法使测试失败,并显示错误消息

Failedassertinglastcontrollerused<"error “> ....

测试:

$this->dispatch('/controller/action/param');
$this->assertController('controller');
$this->assertAction('action');

所以在这种情况下,我如何获得真正的错误消息冒泡到 PHPUnit,即如果控制器中有错误,我想知道它而不是调用误差控制器。

如果我在 application.ini 中设置 resources.frontController.params.noErrorHandler = 1 ,即使存在错误,测试也会通过,因为控制器和操作仍然会发生,但只是不输出任何内容(我知道我可以在输出中查找断言,但这不是重点 - 我想要原始错误)。

我也尝试在 phpunit.xml 中打开

convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true" 

,但没有效果。

任何指示将不胜感激。

我希望这一切都是有道理的!?

非常感谢。

We are currently putting unit tests around zend framework controllers.

(I've abstracted this code example a little bit, but the idea is the same....)

We've happily managed to make the test fail, with the error message

Failed asserting last controller used <"error"> ....

with the test:

$this->dispatch('/controller/action/param');
$this->assertController('controller');
$this->assertAction('action');

So in this instance, how do I get the real error message to bubble up to PHPUnit, i.e. if there is an error in the controller, I want to know about it rather than invoking the error controller.

If I set resources.frontController.params.noErrorHandler = 1 in the application.ini, the test passes even though there is an error, because that controller and action still occurs, but just outputs nothing (I know I could look for assertions in the output, but that's not the point - I want the original error).

I've tried turned on

convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true" 

in phpunit.xml as well, no joy.

Any pointers would be much appreciated.

I hope all of that made sense!?

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

GRAY°灰色天空 2024-11-14 14:04:14

不幸的是,Zend_Test_PHPUnit_ControllerTestCase 在调度期间显式覆盖了一些前端控制器选项。相关的代码在这里:

$controller = $this->getFrontController();
$this->frontController
     ->setRequest($request)
     ->setResponse($this->getResponse())
     ->throwExceptions(false)
     ->returnResponse(false);

因为这是在dispatch()内部完成的,所以你绝对没有机会改变选项。

我使用的一种解决方案是创建我自己的基本 ControllerTestCase 来扩展 Zend 并从 Zend 对象复制整个dispatch() 函数,然后更改这一行。

虽然很乱,但是很有效。您可以做一个更复杂的版本,从配置等中读取一些内容。

Unfortunately Zend_Test_PHPUnit_ControllerTestCase explicitly overrides some frontcontroller options during dispatch. The relevant bit of code is here:

$controller = $this->getFrontController();
$this->frontController
     ->setRequest($request)
     ->setResponse($this->getResponse())
     ->throwExceptions(false)
     ->returnResponse(false);

Because this is done inside dispatch() you have absolutely no chance of altering the options.

One solution I've used is to create my own base ControllerTestCase that extends the Zend one and copy the entire dispatch() function from the Zend object, then change that one line.

It's messy but it works. You could do a more elaborate version that read something from a config etc.

孤寂小茶 2024-11-14 14:04:14

这也是我遇到过的一个问题,有时很难追踪到错误。我偶然发现的一个解决方案 - 我修改了错误控制器,以便在生产环境中出现错误时发送电子邮件,但我不小心将其设置为在测试环境中发送 - 然后当其中一个错误发生在单元测试中,我收到了一封详细说明该错误的电子邮件。

您不必使用电子邮件通知,但您可能需要查看 http://framework.zend.com/manual/en/zend.log.writers.html。您可以将错误写入日志文件,然后检查日志文件。

This is an issue I've run into as well, and it can be pretty difficult sometimes to track down the error. One solution I stumbled on by chance -- I modified the error controller to send an e-mail in the event of an error in our production environment, but I accidentally had it set to send in the testing environment as well -- and then when one of these errors happened on a unit test, I got an e-mail detailing the error.

You don't have to use e-mail notification, but you might want to look into error logging at http://framework.zend.com/manual/en/zend.log.writers.html. You could have the errors written to a log file and then check the log file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文