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