如何 PHPUnit 断言函数 [英] HowTo PHPUnit assertFunction

查看:21
本文介绍了如何 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.

这篇关于如何 PHPUnit 断言函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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