【发布时间】:2016-01-30 15:34:27
【问题描述】:
我的问题是。我有一个类,我只想测试我想要测试的函数是在我想要测试的类中调用私有函数。私有函数调用另一个类函数。
我想模拟私有函数,这样我就可以将值返回给调用私有函数的公共函数。我通过创建一个具有相同函数但具有我希望函数返回的值的新类来尝试它。
这是我的代码
//function i want to test
public function checksomedata($objectID){
$config = $this->theprivatefunction($objectID,'id');
return $config;
}
我想模拟的私有函数
private function theprivatefunction($objectID,'id'){
//do some code here. nothing special
//return the value here
}
这是我的测试
public function testCheckObjectAcces() {
$objectID = '12';
$this->testclass->checksomedata($objectID);
}
这是我想调用的类以返回一些值。
public function theprivatefunction(){
$result = "THIS VALUE NEEDS TO BE RETURNED";
return $result;
}
以及我尝试模拟 privte 函数的设置
$this->mockextended = new \stdClass();
$this->mockextended = new MOCKEXTENDEDCLASSES();
$this->testclass->theprivatefunction() = $this->mockextended->theprivatefunction();
在我的设置中,我希望代码认为 $this->testclass->theprivatefunction() 是 $this->mockextended->theprivatefunction();
所以当一个函数调用私有函数时,它需要重定向到$this->mockextended->theprivatefunction();
【问题讨论】:
-
无法使用 PHPUnit 模拟私有方法。 stackoverflow.com/questions/5937845/… 可能会给你一些关于如何解决这个问题的想法。