donglin4636 2015-12-03 10:37
浏览 58
已采纳

使用Laravel / Lumen外观进行测试

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);
  • 写回答

1条回答 默认 最新

  • dqcd84732 2015-12-03 10:54
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?