我目前正在进行一个循环,其中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
答案 0 :(得分:3)
在调用run_ssh_command的对象上调用重试。
所以
describe "rescues Net::SSH::HostKeyMismatch" do
it "resyncs the ssh keys" do
ssh_instance = ssh_class.new
Net::SSH::HostKeyMismatch.any_instance.should_receive(:remember_host!).and_return(true)
ssh_instance.should_receive(:retry)
ssh_instance.run_ssh_command { raise Net::SSH::HostKeyMismatch}
end
end