【发布时间】:2019-06-18 04:19:43
【问题描述】:
我有一个读取外部 API 的 Domoticz 类。
class Domoticz extends HeaterService implements DomoticControllerInterface {
public function getFromApiCurrentHeaterStatus($current_heater)
{
$current_heater = json_decode($current_heater);
if (!isset($current_heater->result)) {
throw new \Exception('There is a problem retrieving heater status');
}
$heater = array();
foreach ($current_heater->result as $result) {
$heater['idx'] = (int)$result->idx;
$heater['status'] = $result->Status;
}
return $heater;
}
}
方法getFromApiCurrentHeaterStatus返回一个数组。
这是我的测试
class DomoticzTest extends TestCase
{
/**
* This is a copy of real json got from an API call
*/
private $heater_answer = '{"result":{"2":{"Status":"Off","idx":"17"}}}';
/**
* Test that from an answer as $heater_answer, we can get an array with idx and status
*
*/
public function testThatWeCanGetAnArrayFromHeaterAnswer()
{
$right_heater_array = array('idx' => 17, 'status' => 'Off');
$right_array_we_want = $right_heater_array; // we need to get an array
$wrong_heater_array = array('idx' => 11, 'status' => 'On');
$wrong_array_we_dont_want = $wrong_heater_array;
$mock = $this->getMockBuilder('\App\Libs\Domoticz')
->disableOriginalConstructor()
->getMock();
$mock
->expects($this->once())
->method('getFromApiCurrentHeaterStatus')
->with($this->heater_answer)
->willReturn($right_array_we_want);
$heater = $mock->getFromApiCurrentHeaterStatus($this->heater_answer);
$this->assertEquals($right_array_we_want,$heater);
}
测试通过。实际上,通过“真正的”API 调用 ($this->heater_answer),我们得到了一个数组
$heater['idx'] = 17;
$heater['status'] = 'Off';
现在,我确实尝试更改属性 heater_answer,例如将 idx 从 17 更改为 2,或者状态,在每种情况下测试都通过了。
换句话说:真正的方法是不是没有执行?如何强制测试执行真正的方法?
【问题讨论】: