0
我試圖建立一個查詢外部API的類。與端點相對應的每種方法都會調用負責實際向API發送請求的「主調用」方法。測試中phpspec一類涉及狂飲
例如:
// $this->http is Guzzlehttp\Client 5.3
public function call($httpMethod, $endpoint, array $parameters = [])
{
$parameters = array_merge($parameters, [
'headers' => [
'something' => 'something'
]
]);
$request = $this->http->createRequest($httpMethod, $this->baseUrl . $endpoint, $parameters);
return $this->http->send($request);
}
public function getAll()
{
return $this->call('GET', 'all');
}
什麼我我應該嘲笑?我應該使用的HTTP客戶端的createRequest()
和send()
方法willBeCalled()
和/或willReturn()
?
當我嘲笑send()
,它說:Argument 1 passed to Double\GuzzleHttp\Client\P2::send() must implement interface GuzzleHttp\Message\RequestInterface, null given
,我不知道如何爲提供假的,因爲在創建該接口的虛擬要求我實施這個類的30種方法。
這裏的測試現在:
function it_lists_all_the_things(HttpClient $http)
{
$this->call('GET', 'all')->willBeCalled();
$http->createRequest()->willBeCalled();
$http->send()->willReturn(['foo' => 'bar']);
$this->getAll()->shouldHaveKeyWithValue('foo', 'bar');
}
我想我試過類似的東西。問題是,如果我輸入'GuzzleHttp \ Message \ RequestInterface $ request'並將它傳遞給'send()',它會說「你需要實現一個bajillion方法」。我應該在實現所有這些方法的spec類下面創建一個虛擬類嗎? – johnRivs
我嘗試了這種方法,現在我需要一件事。在我的代碼中,我需要返回'$ this-> http-> send($ request) - > json()',但phpspec告訴我它找不到它。目前,我有'$ http-> send($ request) - > json() - > shouldBeCalled()'。 – johnRivs
呦需要模擬什麼'send'回報:'$這個 - > HTTP->發送($要求) - > shouldBeCalled() - > willReturn($不論)'和'$ whatever-> JSON() - > shouldBeCalled ()' – gvf