0

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

/**
 * @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 文档中,他们说每个 Facade 都可以直接模拟,而不需要实例化它,但是测试永远不会通过,我遇到了一个异常:

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

PS:我有 Laravel 5,在测试类中我有 tearDown 方法,以防你想知道。

4

1 回答 1

1

终于找到了解决办法。

我没有使用 File,facade,FileSystem而是通过构造函数注入了依赖关系,并在单元测试中对其进行了模拟,并将模拟对象传递给IoC Container,只有这样才能工作,否则在 Laravel 5 上,Mocking Facades 不起作用,shouldReceive()正如 Laravel Docs 告诉我们的那样,它没有计数。

亲切的问候

于 2014-11-24T17:45:05.990 回答