gpt4 book ai didi

PHPUnit:模拟一种表现得像身份的方法

转载 作者:可可西里 更新时间:2023-10-31 23:38:32 25 4
gpt4 key购买 nike

使用 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 of returnValue().

例如:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com