CrtlAltDylan
4 years ago

Unable to mock the Config::get('configuration.here'); Facade method

Posted 4 years ago by CrtlAltDylan

All of the other Facade mocks work great, but this Config one in particular is acting as if it's not in test mode. I'll show you what I mean:

class Stub
{
      public function someAction()
      {
            return $someConfig = config('appName.some.configuration');
            // works great, I can set and get all kinds of configurations from /config/app/appName.php
      }
 }

However when I try to test my fancy Stub class:

class StubTest extends \Codeception\TestCase\Test
{
       protected function _before()
       {
             $this->stub = new Stub();
       }

       public function testStubSomeActionUsesMyApplicationsConfig()
       {
             \Config::shouldReceive('offsetGet')->with('appName.some.configuration')->andReturn('Yay, mocked!');

             $this->assertEquals('Yay, mocked!, $this->stub->someAction());
       }
}

You would expect this to be a passing test. However when the Config is being mocked, it begins to ask for other configuration variables that this test shouldn't care about:

 [Mockery\Exception\NoMatchingExpectationException]                                                                                     
  No matching handler found for Mockery_2_Illuminate_Config_Repository::offsetGet("database.default"). Either the method was unexpected  or its arguments matched no expected argument list for this method 

If you start to mock the configuration for database.default, it'll move onto your cache configuration. On top of that it asks for real values. It's a waste of time and not sustainable. Has anyone else run into this issue when mocking the Config facade?

Please sign in or create an account to participate in this conversation.