我的 __construct 中有这段代码:
public function __construct(Guard $auth)
{
$this->auth = $auth;
$this->dbUserService = app()->make('DBUserService');
}
现在,当我进行单元测试时,我知道我可以模拟 Guard 并将其模拟传递给 $auth,但我如何模拟 dbUserService
?它通过 IoC 容器实例化。
最佳答案
您可以使用 IoC 容器的 instance()
方法来模拟任何使用 make()
实例化的类:
$mock = Mockery::mock(); // doesn't really matter from where you get the mock
// ...
$this->app->instance('DBUserService', $mock);
关于php - 使用 app()->make 创建的模拟类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29349301/