Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.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 如何在测试中覆盖标记了具体实现的Laravel服务容器?_Php_Laravel_Phpunit - Fatal编程技术网

Php 如何在测试中覆盖标记了具体实现的Laravel服务容器?

Php 如何在测试中覆盖标记了具体实现的Laravel服务容器?,php,laravel,phpunit,Php,Laravel,Phpunit,我有一个接口,目前只有一个实现类。在Laravel服务容器中,我已针对接口标记了该具体类 public function register() { $this->app->tag([ ConcreteClassA::class, ], Interface::class); } 我想用该接口的模拟实现完全覆盖该标记。我尝试了以下方法: $mock = $this->mock(Interface::c

我有一个接口,目前只有一个实现类。在Laravel服务容器中,我已针对接口标记了该具体类

    public function register()
    {
        $this->app->tag([
            ConcreteClassA::class,
        ], Interface::class);
    }
我想用该接口的模拟实现完全覆盖该标记。我尝试了以下方法:

$mock = $this->mock(Interface::class, function (MockInterface $mock) {
            $mock->shouldReceive('someMethod')
                ->andReturn(Carbon::now());
        });

app()->tag([$mock], Interface::class);

我还使用了
binding()
instance()
的不同变体,但运气不好。

我建议使用instance()


您也可以使用tag方法,但随后将有两个具体的类绑定到一个接口。最后,如果您使用make方法解析接口,您将得到第二个类,即mock类。

我建议使用instance()

您也可以使用tag方法,但随后将有两个具体的类绑定到一个接口。最后,如果您使用make方法解析接口,您将得到第二个类,即mock类

$this->app->instance(Interface::class, Mockery::mock(Interface::class, function($mock){
            $mock->shouldReceive("someMethod")->andReturn(Carbon::now());
        }));

$mocked = $this->app->make(Interface::class);
$mocked->someMethod()