Skip to content
Advertisement

Mocking the response only for a certain parameter

I have a class where I am mocking the get method when called with test1 to return 123. This works fine.

However, I want to have all other calls to the get method return what would normally be returned – i.e. only a call with a specific parameter returns a mock response.

$configMock = m::mock(Config::class);

$configMock->shouldReceive('get')
        ->with('test1')
        ->andReturn(123);

So if I call get with a different parameter in the code i.e. $config->get('test2') I get the error

MockeryExceptionNoMatchingExpectationException: No matching handler found for Mockery_1_Illuminate_Contracts_Config_Repository::get(“test2”). Either the method was unexpected or its arguments matched no expected argument list for this method

However when I use ->makePartial() on the first line, at the line $config->get('test2') I get the error

BadMethodCallException: Method Mockery_1_Illuminate_Contracts_Config_Repository::get() does not exist on this mock object

How can I mock a method response for only a certain parameter being passed whilst letting that method return normal responses for all other calls to that method?

Advertisement

Answer

I ended up taking @Loeks advice. There might be a cleaner way to do this but this is working for me.

    $config = new Config;

    $closure = function ($arg) use ($config) {
        switch ($arg) {
            case 'test1':

                return 123;
            default:
                // Return default values

                return $config->get($arg);
        }
    };

    $configMock = m::mock(Config::class)
        ->makePartial();

    $configMock->shouldReceive('get')
        ->andReturnUsing($closure);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement