简体   繁体   中英

Testing with Laravel/Lumen facade

So, I'm trying to write a unit test for a piece of code that uses a facade, and it looks like this:

public function test() {
  MYFACADE::doSomething();
}

and in the unit test, I am trying to mock the facade call:

MYFACADE::shouldReceive('doSomething')
    ->once()
    ->andReturn(false);

The only problem with this is that when Laravel tries to make an instance of underling class for the MYFACADE , it will of course run the constructor, and right there, is hardcoded database connection call. So without changing my facade, and removing database connection from the constructor of the facade class, is there any way to mock facade call without running the constructor of facade ?

UPDATE: Rest of the setup:

Facade class

class MyFacade extends Facade 
{
    protected static function getFacadeAccessor() 
    { 
        return 'myfacade'; 
    }
}

app.php

$app->register(App\Providers\MyServiceProvider::class);
class_alias('App\Facades\MyFacade', 'MYFACADE');

Service provider:

class MyServiceProvider extends ServiceProvider
{

    public function register()
    {

        $this->app->bind('myfacade', function()
        {
            return new myClasThatDoesSomething();
        });
    }
}

The underlying class used by facade

class myClasThatDoesSomething
{
   public function __construct()  
    {
       // opens db connection here  
    }

    public function doSomething()
    {

    }
}

example class that uses facade:

class TestClass 
{
   public function testMethod()
   {
       $returnValue = MYFACADE::doSomething();
       return $returnValue;
   }
}

Unit test that checks if the testMethod() returns 'testValue';

MYFACADE::shouldReceive('doSomething')
            ->once()
            ->andReturn('testValue');   

$instance = new TestClass();
$value = $instance->testMethod();
$this->assertEquals('testValue', $value);

First of all Laravel documentation reads:

Facades provide a "static" interface to classes that are available in the application's service container.

Please make sure your MYFACADE follows this pattern. There is no room for constructor, databases, etc. If you test MYFACADE::doSomething() you should mock all other classes that are being used by the function.

Secondly, the following piece of code

MYFACADE::shouldReceive('doSomething')
    ->once()
    ->andReturn(false);

mocks the facade itself to test something else that uses MYFACADE::doSomething() . It should return an instance of Mockery which returns false wherever MYFACADE::doSomething() is being called within the test.

EDIT:

Laravel's Facade mock implementation instantiates the underlaying class, which allow to test services with thin constructors. Although it is the best practice, it may not always be possible to do. Assuming you cannot move DB connection logic from the constructor, your best bet will be to mock the service manually:

public function testTestMethod()
{
    MYFACADE::swap(\Mockery::mock()
        ->shouldReceive('doSomething')
        ->once()
        ->andReturn('testValue')
        ->getMock()
    );

    $instance = new \App\TestClass();
    $value = $instance->testMethod();
    $this->assertEquals('testValue', $value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM