【发布时间】:2020-12-09 19:51:08
【问题描述】:
我正在为 Service 类编写一个测试。如下所示,我的 Service 类使用 Gateway 类。我想在我的一项测试中模拟 Gateway 类上 getSomething() 的输出。
我尝试使用存根 (createStub) 和模拟 (getMockBuilder),但 PHPUnit 没有产生预期的结果。
我的课:
<?php
class GatewayClass
{
private $client = null;
public function __construct(Client $client)
{
$this->client = $client->getClient();
}
public function getSomething()
{
return 'something';
}
}
<?php
class Service
{
private $gateway;
public function __construct(Client $client)
{
$this->gateway = new Gateway($client);
}
public function getWorkspace()
{
return $this->gateway->getSomething();
}
}
(这个项目还没有DI容器)
【问题讨论】: