phpunit - 测试方法的mockup变量(其他类对象)。

时间:2018-04-18 10:06:43

标签: php mocking phpunit

我想测试函数myFunction,它会创建一些其他对象。

例如:

class sourceClass{
    function myFunction(){
        $other_object = new otherClass();
        if($other_object->someMethod() == 'want to controll it'){
            return 'want this to get';
        }
    }
}

然后我想测试myFunction():

class StubTest extends TestCase{
    function testMyFunction(){
        $this->sourceClass = new sourceClass();
        $this->assertEquals('want this to get', $this->sourceClass->myFunction()));
    }
}

因为函数someMethod属于其他类我不容易(如果有的话)控制它返回的内容。这是我正确运行测试所需要做的。或者如果我可以以某种方式模拟在myFunction中生成的$ other_object变量。 有可能吗?

0 个答案:

没有答案
相关问题