如何将对象传递给 laravel 中的测试方法?
how to pass object to test method in laravel?
我有一个 class 签名如下:
class Service
{
public function __construct(
Class $a,
Class $b,
Class $c
)
如何在测试方法中获取它的实例?通过构造函数的调用抛出以下错误
Too few arguments to function Service::__construct(), 0 passed
但在应用程序本身中,此 class 有效,例如,在命令中:
class ServiceCommand extends AbstractCommand
{
protected $service;
public function __construct(Service $service)
{
parent::__construct();
$this->service = $service;
}
如何在 phpunit 测试方法中获取服务对象的实例?
but in the application itself, this class works
您需要了解依赖注入以及 Service container 在 Laravel 中的工作原理。
如何在 phpunit 测试方法中获取服务对象的实例
两种方式 - 一种用于功能测试,一种用于单元:
特征
使用服务容器注入依赖。
$this->service = $this-app->make(Service::class);
单位
模拟你的依赖 - 阅读更多关于 mocking
$mockA = $this->createMock(Class::class);
// ...
$this->service = new Service($mockA...);
我有一个 class 签名如下:
class Service
{
public function __construct(
Class $a,
Class $b,
Class $c
)
如何在测试方法中获取它的实例?通过构造函数的调用抛出以下错误
Too few arguments to function Service::__construct(), 0 passed
但在应用程序本身中,此 class 有效,例如,在命令中:
class ServiceCommand extends AbstractCommand
{
protected $service;
public function __construct(Service $service)
{
parent::__construct();
$this->service = $service;
}
如何在 phpunit 测试方法中获取服务对象的实例?
but in the application itself, this class works
您需要了解依赖注入以及 Service container 在 Laravel 中的工作原理。
如何在 phpunit 测试方法中获取服务对象的实例
两种方式 - 一种用于功能测试,一种用于单元:
特征
使用服务容器注入依赖。
$this->service = $this-app->make(Service::class);
单位
模拟你的依赖 - 阅读更多关于 mocking
$mockA = $this->createMock(Class::class);
// ...
$this->service = new Service($mockA...);