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

规格:

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 回答