Testing Mockery无法对Facade上的方法调用进行计数

Testing Mockery无法对Facade上的方法调用进行计数,testing,laravel,phpunit,mockery,laravel-5,Testing,Laravel,Phpunit,Mockery,Laravel 5,一个简单的测试有一个根本不通过的问题。 我在控制器中有一个操作: /** * @Get("/parse") * @param Dispatcher $dispatcher * @return string */ public function parse(){ $xml_file = public_path()."/dummy.xml"; //File::get($xml_file); Tried this as well $file = $this->fi

一个简单的测试有一个根本不通过的问题。 我在控制器中有一个操作:

/**
 * @Get("/parse")
 * @param Dispatcher $dispatcher
 * @return string
 */
public function parse(){
    $xml_file = public_path()."/dummy.xml";
    //File::get($xml_file); Tried this as well
    $file = $this->file->get($xml_file);
    return $file;
}
在测试中,我有一个类似的方法:

/**
 * A basic functional test example.
 *
 * @return void
 */
public function testBasicExample(){
        File::shouldReceive("get")->once();
        $this->call('GET', '/parse');
}
在Laravel文档中,他们说可以直接模拟每个外观,而无需实例化它,但测试永远不会通过,我得到一个例外:

Mockery\Exception\InvalidCountException: Method get() from Mockery_0 should be called exactly 1 times but called 0 times.

PS:我有Laravel 5,在测试课上,我有撕裂方法,以防你想知道。

终于找到了解决方案

我没有使用facade文件,而是通过构造函数注入了
文件系统
依赖项,并在单元测试中模拟了该依赖项,并将模拟对象传递到IoC容器,只有这样,模拟facade才起作用,否则在Laravel 5上,模拟Facades不起作用,
shouldReceive()
没有像Laravel Docs告诉我们的那样保持计数


亲切问候

如何将
$this->file
注入控制器?通过构造函数,Laravel通过Dependency Injector完成其余工作,但如果我尝试使用file::get(),这永远不起作用当然是通过构造函数,我指的是如何在
服务提供商
中绑定的代码。这是默认的Laravel外观,使用alias文件,并从IoC解析Illumb\Filesystem\Filesystem