retry
現在、テスト内で実行されるループに遭遇しています。retry
その上のコードをスタブしてテストするにはどうすればよいですか?
HostKeyMismatch 例外をキャッチするスニペットがあります。
rescue Net::SSH::HostKeyMismatch => e
e.remember_host!
retry
end
私の仕様:
describe "rescues Net::SSH::HostKeyMismatch" do
it "resyncs the ssh keys" do
Net::SSH::HostKeyMismatch.any_instance.should_receive(:remember_host!).and_return(true)
ssh_class.new.run_ssh_command { raise Net::SSH::HostKeyMismatch }
end
end
私が得ているエラー:
The message 'remember_host!' was received by #<Net::SSH::HostKeyMismatch: Net::SSH::HostKeyMismatch> but has already been received by Net::SSH::HostKeyMismatch
アップデート:
以下の推奨される回答にカウンターを追加することで、問題を解決できました。
describe "rescues Net::SSH::HostKeyMismatch" do
it "resyncs the ssh keys" do
exception = Net::SSH::HostKeyMismatch.new
exception.should_receive(:remember_host!).and_return(true)
count = 0
ssh_class.new.run_ssh_command do
count += 1
raise exception unless count > 1
end
end
end