我需要测试我编写的某些代码多次调用另一个类上的方法时的行为,其中一次调用将导致抛出异常。
我正在使用 Mockery 来模拟可能引发异常的类。
所以在我的例子中,该方法将被调用三次,我需要它在第二次抛出异常。
这是我的意图的例子,但它不起作用。
$mock = \Mockery::mock();
$mock->shouldReceive('fetch')
->andReturnUsing(
function () {return true;},
function () use ($e) {throw new \Exception();},
function () {return false;}
);
我的印象是,上述内容可能适用于 断言模拟会抛出异常 · 问题 #308 · 嘲笑/嘲笑。
但是,实际上,以这种方式抛出异常会导致 Mockery 捕获异常并抛出自己的
BadMethodCall
异常。
我在 Mockery Github 问题中找到了答案,使用 return 和 throw 模拟多个方法调用。
$mock = \Mockery::mock();
$mock->shouldReceive('fetch')
->andReturnUsing(
function () use () {
static $counter = 0;
switch ($counter++) {
case 0:
return true;
break;
case 1:
throw new \Exception();
break;
default:
return false;
break;
}
}
);
对于那些寻找使用 PHPUnit 的解决方案的人...
$mockHydrator = $this->createMock(MyObject::class);
$mockHydrator->method('fetch')
->will(
$this->onConsecutiveCalls(
true,
$this->throwException($e),
false
)
);
在这种情况下,我觉得 PHPUnit 模拟提供了比 Mockery 更好的界面。
对于 PHPUnit 的最新版本,您可以使用
willReturnCallback
来实现该行为。这是一种为后续调用配置它的通用方法:
$consecutiveReturnValues = [true, null, false];
$count = 0;
$mockHydrator->method('fetch')->willReturnCallback(
function () use ($consecutiveReturnValues, &$count) {
$returnVal = $consecutiveReturnValues[$count] ?? null;
$count++;
if ($returnVal != null) {
return $returnVal;
}
// if the return value is null, we need to throw
throw new Exception();
}
);
您还可以调整条件,而不是使用 array_key_exists 或类似的 null 值。