我正在使用 Guzzle 5.3,希望测试我的客户端是否会抛出 TimeOutException
异常。
那么,我该如何模拟 Guzzle 客户端抛出 GuzzleHttp\Exception\ConnectException
异常呢?
测试代码:
public function request($namedRoute, $data = [])
{
try {
/** @noinspection PhpVoidFunctionResultUsedInspection */
/** @var \GuzzleHttp\Message\ResponseInterface $response */
$response = $this->httpClient->post($path, ['body' => $requestData]);
} catch (ConnectException $e) {
throw new \Vendor\Client\TimeOutException();
}
}
更新:
正确的问题是:如何在Guzzle 5中抛出异常?或者,如何测试使用Guzzle 5的catch块?
httpClient
中设置,以及连接服务器。我认为当我有httpClient
的模拟(Guzzle)时,创建/模拟一个服务器不是一个好主意。只需了解如何使用此模拟抛出异常即可。 - Victor Aguilar