Mock Laravel's Config facade to return a value only for a certain key
Asked Answered
S

1

13

I would like to mock Config::get('specific_key') to return a 'specific_value' in my test. So I wrote the following code:

Config::shouldReceive('get')
    ->with('specific_key')
    ->andReturn('specific_value');
Config::makePartial();

This will work: if I add dd(Config::get('specific_key')) I will receive 'specific_value'.

However, if I do dd(Config::get('another_key')), I don't receive any value (I guess because the mock doesn't expect this key as an argument).

So my question is: Is there a way to mock Config's get() method to return a specific value only for an specific key (and return normal value from the configuration file for any other key)?

Selina answered 2/11, 2017 at 13:28 Comment(0)
M
23

You don't have to mock Config, you can use Config::set() to set any value in Config. So Config::set('specific_key', 'specific_value'); in the test instead of creating the mock should work

Mariomariology answered 2/11, 2017 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.