0

仕様で Net::SFTP::StatusException エラーを強制的に発生させ、コードがそれをトラップすることを検証しようとしています。

コード:

def process_SFTP(username, password, csvfile)
  begin
      mySftpWrapper.new
      mySftpWrapper.process_CSV_file(csvfile)
  rescue RuntimeError => other_problem
      logger.info(other_problem.message)
  rescue Net::SFTP::StatusException => sftp_problem
      logger.info(sftp_problem.message)
  end
end

R仕様:

let(:wrapper) { mySftpWrapper.new }
before(:all) do
    @wrapper_mock = double('mySftpWrapper')
    mySftpWrapper.stub(:new).and_return(@wrapper_mock)
    @logger_mock = double('ActiveSupport::BufferedLogger')
end

it "should trap a Net::SFTP::StatusException" do
     sftp_response = Object.new
      def sftp_response.code; code = 2 end
      def sftp_response.message; message = "no such file" end
      # Force exception here
      @wrapper_mock.stub(:process_SFTP).with("username", "password", "nonexistentfile").and_raise(Net::SFTP::StatusException.new(sftp_response))
      # Verify that logger.info received the forced error
      @logger_mock.stub(:info).should_receive(/no such file/)
      wrapper.process_SFTP("date", "username", "password", "nonexistentfile")
end

ただし、Net::SFTP::StatusException を生成するために使用するステートメントは、正しい例外 ( ) をスローしません"Net::SFTP::StatusException"。代わりにスローします#<Net::SFTP::StatusException: Net::SFTP::StatusException>

正しい StatusException を強制するにはどうすればよいですか?

どんなアイデアでも大歓迎です!

4

1 に答える 1

0

結局のところ、仕様の例は正確に正しい例外をスローしています。問題は、Net::SFTP::StatusException が RuntimeError からサブクラス化されていることです。

net-sftp-2.0.5/lib/net/sftp/errors.rb:

module Net; module SFTP

  # The base exception class for the SFTP system.
  class Exception < RuntimeError; end

  # A exception class for reporting a non-success result of an operation.
  class StatusException < Net::SFTP::Exception

Net::SFTP::StatusException がスローされていましたが、StatusException 句に到達する前に RuntimeError 句によってトラップされていました。まさにそのスペック!

于 2012-08-29T18:00:39.930 に答える