我有一个关于使用PHPUnit模拟类中的私有方法的问题.让我举一个例子:
class A {
public function b() {
// some code
$this->c();
// some more code
}
private function c(){
// some code
}
}
Run Code Online (Sandbox Code Playgroud)
如何将私有方法的结果存根以测试公共函数的更多代码部分.
解决了部分阅读在这里
使用PHPUnit和PHP> = 5.3,可以测试受保护的方法.stackoverflow的以下页面概述了它的最佳实践:
protected static function callProtectedMethod($name, $classname, $params) {
$class = new ReflectionClass($classname);
$method = $class->getMethod($name);
$method->setAccessible(true);
$obj = new $classname($params);
return $method->invokeArgs($obj, $params);
}
Run Code Online (Sandbox Code Playgroud)
使用PHPUnit可以轻松地在抽象类上测试公共方法.使用上述方法可以轻松地测试正常类上的受保护方法.要测试抽象类上的受保护方法必须以某种方式...
我知道PHPUnit派生抽象类并在具体类中"实现"抽象方法并针对该具体类触发测试 - 但我不知道如何将其集成到上面的方法中以获得callProtectedMethodOnAbstractClasses().
你是怎么做这样的测试的?
PS:问题不在于测试受保护方法的真相(参见:白色,灰色和黑盒测试).测试受保护方法的需要取决于您的测试策略.