我有一个简单的测试:
function it_should_return_error_response_exception(Client $httpClient,CommandInterface $commandInterface)
{
$httpClient->setDefaultOption('auth', array('api','api_pass', 'Basic'))
->shouldBeCalled();
$httpClient->getCommand('search', array('api_key' => 'ehudwqukhjda'))
->shouldBeCalled()
->willReturn($commandInterface);
$httpClient->execute($commandInterface)
->shouldBeCalled()
->willThrow(new BadResponseException('???', new Request('POST', 'http://vatteloapesca')));
$this->shouldThrow('Acme\Exception\ErrorResponseException')
->during('runCommand', array('search', array('api_key' => 'ehudwqukhjda')));
}
这是我要测试的代码:
try{
$result = $this->guzzleClient->execute($command);
} catch (BadResponseException $e) {
ErrorHandler::processError($e);
}
return $result;
它已经过测试的错误处理程序类,将返回一个扩展' Acme \ Exception \ ErrorResponseException'的类。问题是,如何从guzzle客户端模拟返回的Exception?
我试图使用预言的{4}}和/或
的willTrhow和ThrowPromises我的错误是什么?
我的意思是,使用此代码:
$httpClient->execute($commandInterface)
->shouldBeCalled()
->willThrow(new BadResponseException('???', new Request('POST', 'http://vatteloapesca')));
' runCommand' (测试的功能)它将返回BadResponseException但它没有被我的代码捕获。
答案 0 :(得分:2)
你可以这样做:
使用规范顶部的例外:
use CRMPicco\Bundle\Exception\ImageImportDirectoryUnavailableException;
$this->shouldThrow(ImageImportDirectoryUnavailableException::class)
->during('importImageAssets', [$imageImportPath]);
......并从你的代码中抛出它:
public function importImageAssets($importDirectory)
{
$filesystem = new Filesystem();
if (false === $filesystem->exists($importDirectory)) {
throw new ImportDirectoryUnavailableException();
}
// ...
}