PHPUnit Mock - 将返回对象

5
使用phpunit模拟对象,我有一个返回对象的方法。
如何使用expects/method/will方法编写这个方法?
例如:
 ->will($this->returnValue('Class_Name'));

你所需的一切都在这里:http://www.phpunit.de/manual/3.8/en/test-doubles.html。(在你的例子中,模拟将只返回一个字符串,“returnValue”需要传递一个对象,如果你想它会被返回。) - Cyprian
请转到以下网址以获取有关测试替身的更多信息:https://phpunit.de/manual/current/en/test-doubles.html。 - jsh
1个回答

11
创建对象,并使用 returnValue() 函数将其返回。例如:
$myObject = new RandomObject();
$myFactory = $this->getMock('ObjectFactory', array('getRandomObject'));
$myFactory->expects($this->any())->method('getRandomObject')->will($this->returnValue($myObject);

$this->assertInstanceOf('RandomObject', $myFactory->getRandomObject());

这会过去的。

您还可以将该对象本身创建为模拟对象并传递模拟对象。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接