Mockery specifying expected arguments for multiple calls
Asked Answered
M

1

25

I am trying to mock an object that gets two calls to the same function but with different arguments. It's pretty straight forward to give back different return values for multiple calls but I can't find anywhere how to do it with the argument validation.

I tried:

$this->eventDispatcher
    ->shouldReceive('dispatch')
    ->twice()
    ->with(Events::SELECT,\Mockery::type('\Not\Really\A\Namespace\Event'))
    ->with(Events::ACTIVITY,\Mockery::type('\Not\Really\A\Namespace\Event');

And

$this->eventDispatcher
        ->shouldReceive('dispatch')
        ->twice()
        ->with(
            [Events::SELECT,\Mockery::type('\Not\Really\A\Namespace\Event')],
            [Events::ACTIVITY,\Mockery::type('\Not\Really\A\Namespace\Event')]
        );

But they don't work.

From the output PHPUnit gives me it seems like I'm getting an array?

Mannikin answered 22/10, 2014 at 14:32 Comment(0)
M
31

Well that was fast ;P Apparently you can do this and it works just fine:

$this->eventDispatcher
    ->shouldReceive('dispatch')
    ->with(Events::SELECT,\Mockery::type('\Not\Really\A\Namespace\Event'));

$this->eventDispatcher
    ->shouldReceive('dispatch')
    ->with(Events::ACTIVITY,\Mockery::type('\Not\Really\A\Namespace\Event'); 
Mannikin answered 22/10, 2014 at 14:46 Comment(1)
I found that you have to add once() after each shouldReceive() otherwise the test will not fail if the method is not called at all. I also had to add another shouldReceive()->never() at the end to not allow further calls. Feels a bit hacky - but seems to work.Danilodanio

© 2022 - 2024 — McMap. All rights reserved.