如何在另一个调用的函数上使用 shouldReceive?

How do I use shouldReceive on a function that was called by another?

我有这样的功能:

// $value could be an array or SomeClass type
public function foo($key, $value) 
{
    // some code
    if ($value instanceof SomeClass) {
        $value = $this->bar($value);
    }
    // some code
}

protected function bar(SomeClass $value) 
{
    // do stuff
}

现在在我的测试中我有这样的东西:

{
    $suppliedValue = [];

    $mock = ... // create mock
    $mock->shouldReceive('foo')->withArgs(
        // first arg should be an int or string
        // second arg should be an array or SomeClass object
    );

    if (typeOf($suppliedValue) === 'array') {
        $mock->shouldNotReceive('bar');
    } else {
        $mock->shouldReceive('bar');
    }

    $mock->aFunctionThatCallsFoo($suppliedValue);
}

但是它似乎不起作用,无论向 foo() 提供什么值,它都不会触发 bar() 上的 shouldReceive() / shouldNotReceive()

我在这里错过了什么?我觉得好像我误解了一些关于嘲笑的基本知识。

我没有找到通过模拟解决此问题的方法,但我使用了 Spy


$spy = Mockery::spy(TheClass::class)
              ->makePartial()
              ->shouldAllowMockingProtectedMethods();

$spy->aFunctionThatCallsFoo($suppliedValue);
$spy->shouldHaveReceived('foo')
    ->withArgs(/* args */); 
$spy->shouldHaveReceived('bar');

我最终创建了 2 个测试函数,而不是将 if else 用于 $suppliedValue 类型,其中另一个调用 shouldNotHaveReceived()