I'm currently running into a loop where retry
gets executed within my test. How can I stub retry
and still test the code above it?
I have a snippet to catch HostKeyMismatch exceptions:
rescue Net::SSH::HostKeyMismatch => e
e.remember_host!
retry
end
My spec:
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
Error I'm getting:
The message 'remember_host!' was received by #<Net::SSH::HostKeyMismatch: Net::SSH::HostKeyMismatch> but has already been received by Net::SSH::HostKeyMismatch
Update:
I was able to resolve the issue by adding a counter to the suggested answer below:
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
Retry is called on the object on which your run_ssh_command was called.
So
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.