PHPUnit:死后继续,期待“死”或者以某种方式处理die()? [英] PHPUnit: continue after die, expect "die" or somehow handle die()?

查看:169
本文介绍了PHPUnit:死后继续,期待“死”或者以某种方式处理die()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如果该函数应该杀死PHP,那么如何使用PHPUnit来测试一个函数?

我正在写一些单元测试。我正在测试的系统是一个MVC框架中的网络应用程序。

I'm writing some unit tests. The system I'm currently testing is a web-app in an MVC framework.

如果我们要渲染没有站点皮肤系统的页面,我们传统上运行我们的代码像往常一样,但打印出一个die();声明在功能结束之前退出网站的其余部分呈现。

If we want to render pages without the site-skin system we've traditionally run our code as usual, but printed a "die();" statement at the end of the function to exit before the rest of the website renders.

现在我们正在添加单元测试,这似乎是一个问题。你看,当你DIE();在MVC中,似乎向PHPUnit发送相同的消息。

Well now that we're adding unit testing, this seems to be a problem. You see, when you DIE(); in the MVC, that seems to send the same message to PHPUnit.

Grrrr ...代码现在已经呈现为不可理解

Grrrr... the code has now been rendered "Untestable"

还是有吗?

如何计划一个die();在PHPUnit?

How does one plan for a die(); In PHPUnit?

我知道在测试之前添加:

I know about adding this before a test:

/**
 * @expectedException PHPUnit_Framework_Error
 */

但它没有不允许我们测试这些变量,因为他们在死亡时(至少我不这么认为)。

but it doesn't allow me to test the variables as they were 'at time of death' (at least I don't think so)

任何人都可以启发我?

Anyone able to enlighten me?

谢谢!
:D

Thanks! :D

推荐答案

在这种情况下,您只需要使用 return 而不是 die 。在IDE中可以轻松地将所有死亡替换为返回。

In this case you just need to use return instead of die. It should be easy to replace all the deaths to returns in your IDE.

真正的问题是,当您真的需要测试由退出 die (见edorian的答案)。

The real problem is, when you really need to test application exit code returned by exit or die (see edorian's answer).

这篇关于PHPUnit:死后继续,期待“死”或者以某种方式处理die()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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