如何 PHPUnit 断言函数
我想知道如何验证类"是否具有函数.assertClassHasAttribute 不起作用,这是正常的,因为 Function 不是 Attribute.
I was wondering if how I can verify if a 'class' has a Function. assertClassHasAttribute does not work, it's normal since a Function is not an Attribute.
推荐答案
当 PHPUnit 没有提供断言方法时,我要么创建它,要么使用带有详细消息的低级断言之一:
When there's not an assertion method provided by PHPUnit I either create it or use one of the lower-level assertions with a verbose message:
$this->assertTrue(
method_exists($myClass, 'myFunction'),
'Class does not have method myFunction'
);
assertTrue()
是最基础的.它提供了很大的灵活性,因为您可以使用任何内置的 php 函数来为您的测试返回 bool 值.因此,当测试失败时,错误/失败消息根本没有帮助.类似 Failed asserting that <FALSE>为真
.这就是为什么将第二个参数传递给 assertTrue()
来详细说明测试失败的原因很重要.
assertTrue()
is as basic as you can get. It allows a great deal of flexibility because you can use any built-in php function that returns a bool value for your test. Consequently, when the test fails the error/failure message isn't helpful at all. Something like Failed asserting that <FALSE> is TRUE
. That's why it's important to pass the second param to assertTrue()
detailing why the test failed.
相关文章