【问题标题】:Mockery to define multiple possible values to an expectation嘲笑为期望定义多个可能的值
【发布时间】:2016-07-29 05:42:46
【问题描述】:

我试图根据我希望返回值不同的值声明一个具有两个可能值作为参数的期望。

这是我尝试过的

$mock = m::mock('FooBar\ClassA');
$mock->shouldReceive('has')->with('foo')->andReturn(false);
$mock->shouldReceive('has')->with('bar')->andReturn(true);

我收到此错误

Mockery\Exception\NoMatchingExpectationException:没有匹配的处理程序 找到 Mockery_2__FooBar_ClassA::has("bar")

我读完了http://docs.mockery.io/en/latest/reference/expectations.html

但我无法找到答案。我正在尝试使用andReturnUsing 解决问题,这是直截了当的。有没有不使用andReturnUsing的方法解决问题?

$mock->shouldReceive('has')->andReturnUsing(function ($value) {
    switch ($value) {
    case 'foo': return false;
    case 'bar': return true;
    }
});

我也看了Mockery specifying expected arguments for multiple calls

但这与我尝试过的相同,它强制验证参数类型。

【问题讨论】:

    标签: php mockery


    【解决方案1】:

    好的,我找到了答案

    $mock->shouldReceive('has')->withArgs(['foo'])->andReturnValues([false]);
    $mock->shouldReceive('has')->withArgs(['bar'])->andReturnValues([true]);
    

    因为withArgs 只是with 的包装,我认为也可以使用with,但我认为参数必须在数组中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 2016-11-18
      • 2019-07-07
      • 2014-10-27
      相关资源
      最近更新 更多