我试图在我的规范中强制 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?
任何想法都非常感谢!