使用 PHPUnit,有没有办法模拟一个方法并使其表现得像身份 (x->x)?周围的东西:
$this->myMethod(Argument::any())->willReturn(<should return the argument untouched>)
最佳答案
您可以使用 returnArgument()
方法。来自文档:
Sometimes you want to return one of the arguments of a method call (unchanged) as the result of a stubbed method call. Example 9.4 shows how you can achieve this using
returnArgument()
instead ofreturnValue()
.
例如:
class StubTest extends PHPUnit_Framework_TestCase
{
public function testReturnArgumentStub()
{
// Create a stub for the SomeClass class.
$stub = $this->getMockBuilder('SomeClass')
->getMock();
// Configure the stub.
$stub->method('doSomething')
->will($this->returnArgument(0));
// $stub->doSomething('foo') returns 'foo'
$this->assertEquals('foo', $stub->doSomething('foo'));
// $stub->doSomething('bar') returns 'bar'
$this->assertEquals('bar', $stub->doSomething('bar'));
}
}
关于PHPUnit:模拟一种表现得像身份的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33932987/