将makeWith与模拟结合使用

时间:2018-08-31 10:18:27

标签: laravel testing laravel-5.6 mockery

在我的代码中,我使用makeWith来解决依赖关系。 在我的测试中,我正在创建一个应通过调用makeWith在代码中解决的模拟。但是事实并非如此。

App::make('App\Item'); //ok: returns mock
App::makeWith('App\Item', ['name' => 'apple'];//not ok: doesn't return the mock but the class to be used outside the test environment

这是错误还是我错过了什么?

1 个答案:

答案 0 :(得分:0)

要设置模拟对象,请使用

App::offsetSet('App\Item', $itemMock);

现在

App::makeWith('App\Item', ['name' => 'apple']);

返回$itemMock