Search code examples
unit-testingphpunitmockery

Mockery not working as exected


I am using Mockery to write some tests. But my test is not working as expected. What I want to achieve is to check if the method was called at least once with the specified arguments and ignore the rest.

$m = Mockery::mock();
$m->shouldReceive('update')->with('name', 'Mahad')->atLeast()->once();

$m->update('name', 'Mahad');
$m->update('name', 'Test');

Solution

  • It seems like you need to be explicit about how many times the call should be made and on each expectation statement the counts drop, so, here is how it should be written:

    $m = Mockery::mock();
    $m->shouldReceive('update')->with('name', 'Mahad')->atLeast()->once();
    $m->shouldReceive('update')->times(1);
    
    $m->update('name', 'Mahad');
    $m->update('name', 'Test');