Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)?

like image 580
Armin Sam Avatar asked Nov 02 '17 13:11

Armin Sam


1 Answers

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

like image 75
rypskar Avatar answered Oct 20 '22 22:10

rypskar