One minute
How to Debug Mockery Expectations
Mockery it’s a wide known library to work with mocks in an easy way in PHP.
But as it’s usual with mocks sometimes get very hard to debug because of it’s way of working. It basically works setting some expectations where if we don’t receive the very same expected call to the mock the test it will fail. When that happens we’ll face an error like:
Mockery\Exception\NoMatchingExpectationException : No matching handler found for Mockery_0_SomeService::doSomething(object(SomeObject)). Either the method was unexpected or its arguments matched no expected argument list for this method
That probably is because our mocked service is not receiving the expected data.
That’s indeed that the received SomeObject
doesn’t match with the expectation or that we even haven’t called doSomething
method.
See the expectation:
$this->someServiceMock
->shouldReceive('doSomething')
->once()
->with($expectedObject);
To know what we are exactly receiving we can tune this expectation to the following:
$this->someServiceMock
->shouldReceive('doSomething')
->once()
->with(\Mockery::on(function ($receivedObject) use ($expectedObject): bool {
var_dump($expectedObject);
var_dump($receivedObject);
return true;
}));
If we run the test now we’ll get an output with the two objects which we’ll be able to compare with any text comparasion tool like the one included in PHPStorm. We can even set a break point to see the two values.