Mik*_*e B 37
当PHPUnit没有提供断言方法时,我要么创建它,要么使用一个带有详细消息的低级断言:
$this->assertTrue(
method_exists($myClass, 'myFunction'),
'Class does not have method myFunction'
);
Run Code Online (Sandbox Code Playgroud)
assertTrue()尽可能基本.它允许很大的灵活性,因为你可以使用任何内置的php函数为你的测试返回一个bool值.因此,当测试失败时,错误/失败消息根本没有用.有点像Failed asserting that <FALSE> is TRUE.这就是为什么通过第二个参数来assertTrue()详细说明测试失败的原因很重要.
单元和集成测试用于测试行为,而不是重述类定义的内容.
所以PHPUnit不提供这样的断言.PHPUnit可以断言一个类有一个名字X,一个函数返回值somthing,但你可以使用你想做的事情:
/**
* Assert that a class has a method
*
* @param string $class name of the class
* @param string $method name of the searched method
* @throws ReflectionException if $class don't exist
* @throws PHPUnit_Framework_ExpectationFailedException if a method isn't found
*/
function assertMethodExist($class, $method) {
$oReflectionClass = new ReflectionClass($class);
assertThat("method exist", true, $oReflectionClass->hasMethod($method));
}
Run Code Online (Sandbox Code Playgroud)