phpunit替换方法return

phpunit替换方法return,phpunit,Phpunit,这是我的密码。如何修改函数的返回结果 class Replace { public function add($a){ // how can I replace $this->double($anyNum) return value return $a + $this->double($a); } public function double($a){ return $a + $a; } } class

这是我的密码。如何修改函数的返回结果

class Replace {
    public function add($a){
        // how can I replace $this->double($anyNum) return value
        return $a + $this->double($a);
    }
    public function double($a){
        return $a + $a;
    }
}
class ReplaceTest extends PHPUnit_Framework_TestCase {
    public function testadd(){
        $replace = $this->getMock('Replace');

        // I want to control the method's return, 
        //no matter what num passed to it from function add
        $replace->expects($this->any())->method('double')->will($this->returnValue(15));

        // this return null
        $data = $replace->add(6);

        // this is the expected result I want, 
        // and when I set the returnValue(21),I hope the expected result is 27
        $this->assertEquals(21, $data);
    }
}

如何修改我的代码,非常感谢。

如果指定,getMock的第二个参数告诉phpunit要替换哪些方法。因此,在你的情况下:


虽然在您给出的示例中,我建议不要模拟Replace类,但为double方法添加一个测试就足够了,因为如果double测试通过,您可以依赖该方法的结果。

谢谢$replace=$this->getMock'replace',数组'double';这是正确的。我使用这个例子是因为我想在另一个类中测试move\u uplated\u文件。我将move\u uplated\u文件放在一个公共函数中,比如my\u move\u uplated\u file,然后替换my\u move\u uplated\u file函数的返回,让测试通过。是的,在这种情况下,在同一个对象上存根一个方法是有意义的。
$replace = $this->getMock('Replace',array('add'))