如何在 php 中使用 simpletest 预测致命错误
我正在尝试编写一个测试用例以确保无法实例化 Singleton 类。 Singleton 的构造函数被定义为私有的,因此我的测试如下:
$this->expectError();
$test = new TestSingletonClassA();
我没有捕获错误并通过测试,而是收到“PHP 致命错误:调用 private Singleton::__construct()”。我还尝试将 PatternExpectation 作为参数传递给 ExpectError,但这也不起作用。您有什么建议吗?
一些背景:php5.3,simpletest1.1a
I am attempting to write a test case to ensure a Singleton class cannot be instantiated. The constructor for the Singleton is defined to be private so my test is as follows:
$this->expectError();
$test = new TestSingletonClassA();
Instead of catching the error and passing the test, I get a 'PHP Fatal error: Call to private Singleton::__construct()'. I've also tried passing a PatternExpectation as a parameter to expectError, but that didn't work either. Do you have any suggestions?
Some background: php5.3, simpletest1.1a
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你的 php 代码抛出致命错误,它永远不会到达 phpunit,所以你必须编写“正确”的代码才能测试它。如果调用私有方法,它将抛出异常,因此它不会到达 phpunit。你必须改变这一点。
我认为你必须嘲笑这个对象。尝试关于此主题的这些帖子(这是一系列 4 篇帖子)和这些幻灯片(来自幻灯片 #43)。
If your php code throws a FATAL ERROR, it will never get to the phpunit, so you have to write "right" code in order to test it. If you call a private method, it will throw an excepcion, so it won't get to the phpunit. You have to change that.
I think you have to mock the object. Try these posts about this subject (it's a series of 4 posts) and these slides (from slide #43) .
单元测试框架无法捕获此类内容。但是您可以使用 PHPT 和类似的回归测试框架。
您可能需要跳过一些步骤才能将其连接到 PHPUnit,但可能有一些方法可以将其与其余测试集成。您只需将断言和预期致命错误的特殊情况分开即可。
Unit testing frameworks cannot catch such things. But you can with PHPT and similar regression test frameworks.
You might have to jump through some hoops to hook it into PHPUnit, but there's probably ways to integrate it with the remaining tests. You'll just have to separate assertions and the special case where you expect the fatal error.
我认为不可能这样做......正如我所理解的那样,致命错误是不可捕获的。您可以使用反射来获取构造函数方法,然后确保它具有访问修饰符“private”。
这在大多数语言中都很难测试。例如,java、c# 和 c++ 甚至不允许您编译此代码。所以它根本无法运行 D:
i don't think it's possible to do it that way.. fatal errors are not catchable as i've understood it. you could use reflection to get the constructor method, and then make sure it has the access modifier "private".
this is hard to test in most languages. for example java, c# and c++ would not even let you compile this code. so it could never be run at all D: