0

I'm trying to force an Net::SFTP::StatusException error in my spec and then validate that my code traps it.

Code:

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

RSpec:

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

However, the statement I use to generate the Net::SFTP::StatusException doesn't throw the correct exception ("Net::SFTP::StatusException"). Instead it throws #<Net::SFTP::StatusException: Net::SFTP::StatusException>

How do I force the correct StatusException?

Any ideas are much appreciated!

Sly
  • 743
  • 13
  • 38

1 Answers1

0

As it turns out, the spec example is throwing exactly the correct Exception. The problem is that the Net::SFTP::StatusException is subclassed from 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

and the Net::SFTP::StatusException was getting thrown, but it was getting trapped by the RuntimeError clause before reaching the StatusException clause. Exactly what a spec is for!

Sly
  • 743
  • 13
  • 38