3

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
4

1 に答える 1

4

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
于 2013-10-14T14:53:19.827 に答える