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?
任何想法非常感謝!