How do you mock a static facade methods?

Technology CommunityCategory: LaravelHow do you mock a static facade methods?
VietMX Staff asked 3 years ago

Facades provide a “static” interface to classes that are available in the application’s service container. Unlike traditional static method calls, facades may be mocked. We can mock the call to the static facade method by using the shouldReceive method, which will return an instance of a Mockery mock.

// actual code
$value = Cache::get('key');

// testing
Cache::shouldReceive('get')
                    ->once()
                    ->with('key')
                    ->andReturn('value');