DEV Community

Dimitrios Desyllas
Dimitrios Desyllas

Posted on

Alternate Assertion for Mockery.

Sometimes In order to use Mockery in order to test whether a method id called you can do:

$mockedAnotherService = Mockery::spy(AnotherService::class);
$mockedAnotherService->shouldReceive('SetXY');
// Run code here
$mockedAnotherService->shouldHaveReceived()->setXY(5,10);
Enter fullscreen mode Exit fullscreen mode

But also can be done as:

$mockedAnotherService = Mockery::spy(AnotherService::class);
$mockedAnotherService->shouldReceive('SetXY')->withArgs(function ($x, $y) {
            $assert = ($x == 10 && $y == 20) || ($x == 19 && $y == 26);
            $this->assertTrue($assert);
            return $assert;
 });
Enter fullscreen mode Exit fullscreen mode

As you can see we use phpunit's assertion functions inside the function withArgs so we can assert and tell Mockery whether function call is done as expected or not.

Top comments (0)