2012-08-29 75 views
0

我試圖在我的規範中強制Net :: SFTP :: StatusException錯誤,然後驗證我的代碼陷阱它。RSpec:強制網:: 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 

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 

但是,我用它來生成網聲明:: SFTP :: StatusException不會拋出正確的異常("Net::SFTP::StatusException")。相反它拋出#<Net::SFTP::StatusException: Net::SFTP::StatusException>

如何強制正確的StatusException?

任何想法非常感謝!

回答

0

事實證明,spec規範正在拋出正確的異常。問題是,淨:: SFTP :: StatusException從RuntimeError子類:

淨SFTP-2.0.5/lib目錄/淨/ 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 

和網: :SFTP :: StatusException正在被拋出,但它在到達StatusException子句之前被RuntimeError子句捕獲。究竟是什麼規格!