Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正在模拟使用app()创建的类->;制作_Php_Unit Testing_Laravel_Phpunit - Fatal编程技术网

Php 正在模拟使用app()创建的类->;制作

Php 正在模拟使用app()创建的类->;制作,php,unit-testing,laravel,phpunit,Php,Unit Testing,Laravel,Phpunit,我的结构中有以下代码: public function __construct(Guard $auth) { $this->auth = $auth; $this->dbUserService = app()->make('DBUserService'); } 现在,当我进行单元测试时,我知道我可以模拟Guard并将它的mock传递给$auth,但是如何模拟dbUserService?它是通过IoC容器实例化的 您可以使用IoC容器的instance()方法模

我的结构中有以下代码:

public function __construct(Guard $auth)
{
    $this->auth = $auth;
    $this->dbUserService = app()->make('DBUserService');
}

现在,当我进行单元测试时,我知道我可以模拟Guard并将它的mock传递给$auth,但是如何模拟
dbUserService
?它是通过IoC容器实例化的

您可以使用IoC容器的
instance()
方法模拟与
make()
关联的任何类:


试试
app()->bind('DBUserService',$DBUserServiceMock)
你能详细说明一下吗?这在测试用例中到底会是什么样子?很抱歉,它是
instance()
而不是
bind()
。看我的答案;)
$mock = Mockery::mock(); // doesn't really matter from where you get the mock
// ...
$this->app->instance('DBUserService', $mock);