【发布时间】:2015-06-24 14:29:48
【问题描述】:
我在 PHPUnit 手册中读到,通过以下示例,方法调用 doSomething('a','b','c') 将返回 d,方法调用 doSomething('e','f','g') 将返回 h。
<?php
require_once 'SomeClass.php';
class StubTest extends PHPUnit_Framework_TestCase
{
public function testReturnValueMapStub()
{
// Create a stub for the SomeClass class.
$stub = $this->getMockBuilder('SomeClass')
->getMock();
// Create a map of arguments to return values.
$map = array(
array('a', 'b', 'c', 'd'),
array('e', 'f', 'g', 'h')
);
// Configure the stub.
$stub->method('doSomething')
->will($this->returnValueMap($map));
// $stub->doSomething() returns different values depending on
// the provided arguments.
$this->assertEquals('d', $stub->doSomething('a', 'b', 'c'));
$this->assertEquals('h', $stub->doSomething('e', 'f', 'g'));
}
}
?>
是否还有一种方法可以定义这样的返回值映射,但在特定输入参数没有特定返回值时使用默认返回值?
【问题讨论】: