簡単なテストがあります:
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 クライアントから返された例外をモックする方法です??
私は予言のwillTrhowとThrowPromisesを使用しようとしましたhttps://github.com/phpspec/prophecy
私のエラーは何ですか?
つまり、このコードで:
$httpClient->execute($commandInterface)
->shouldBeCalled()
->willThrow(new BadResponseException('???', new Request('POST', 'http://vatteloapesca')));
'runCommand' (テストされた関数) は BadResponseException を返しますが、私のコードではキャッチされません。