PHPUnit测试断言函数/ var是公共私有还是受保护?

时间:2011-05-09 18:41:25

标签: phpunit

有没有办法断言方法或变量是公共私有还是受phpunit保护?

1 个答案:

答案 0 :(得分:6)

PHPUnit不提供这些断言,您通常不使用单元测试来测试您的输入能力。他们应该验证代码是否在运行时工作。这里有更多毫无意义的单元测试:

  • 断言该类名为CorrectClassName
  • 断言function foo() { return 5; }返回5
  • 断言功能评论不包含“获胜”一词。

现在,有时候你只是想做一些事情,即使它不推荐也没什么价值。我知道我这样做。 :)将此添加到您的基本测试用例类:

/**
 * Assert that a method has public access.
 *
 * @param string $class name of the class
 * @param string $method name of the method
 * @throws ReflectionException if $class or $method don't exist
 * @throws PHPUnit_Framework_ExpectationFailedException if the method isn't public
 */
function assertPublicMethod($class, $method) {
    $reflector = new ReflectionMethod($class, $method);
    self::assertTrue($reflector->isPublic(), 'method is public');
}

完成assertProtectedMethod()assertPrivateMethod()作为练习。你可以为属性做同样的事情,你甚至可以使这个方法更通用来处理一个方法或属性 - 无论哪个找到 - 如果两个都不存在则抛出一些其他错误。

相关问题