[英]RSpec: stubbing SFTP - private method called
I'm attempting to stub out SFTP in a model. 我正在尝试在模型中存根SFTP。 Here's the model:
这是模型:
class BatchTask
require 'net/sftp'
def get_file_stream(host, username, password, path_to_dir, filename)
raise ArgumentError if host.nil? or username.nil? or password.nil? or path_to_dir.nil? or filename.nil?
file_stream = nil
Net::SFTP.start(host, username, password) do |sftp|
sftp.dir.glob(path_to_dir, filename) do |entry|
# Verify the directory contents
raise RuntimeError(true), "file: #{path_to_dir}/#{filename} not found on SFTP server" if entry.nil?
file_stream = sftp.file.open("#{path_to_dir}/#{entry.name}")
end
end
file_stream
end
end
Here's the spec: 规格如下:
require 'spec_helper'
describe "SftpToServer" do
let(:ftp) { BatchTask::SftpToServer.new }
it "should return a file stream" do
@sftp_mock = mock('sftp')
@entry = File.stubs(:reads).with("filename").returns(@file)
@entry_mock = mock('entry')
@entry_mock.stub(:name).and_return("filename")
@sftp_mock.stub_chain(:dir, :glob).and_yield(@entry_mock)
Net::SFTP.stub(:start).and_yield(@sftp_mock)
@sftp_mock.stub_chain(:file, :open).with("filename").and_yield(@file)
ftp.get_file_stream("ftp.test.com", "user", "password", "some/pathname", "filename").should be_kind_of(IO)
end
end
Here's the stacktrace: 这是堆栈跟踪:
NoMethodError in 'SftpToServer should return a file stream'
private method `open' called for #<Object:0x10c572620>
/Users/app/models/batch_task/sftp_to_server.rb:12:in `get_file_stream'
/Users/app/models/batch_task/sftp_to_server.rb:9:in `get_file_stream'
/Users/app/models/batch_task/sftp_to_server.rb:8:in `get_file_stream'
./spec/models/batch_task/sftp_to_server_spec.rb:15:
I've googled this, but I can't figure out why RSpec would treat sftp.file.open as a private method... 我已经用谷歌搜索过,但是我不知道为什么RSpec会将sftp.file.open视为私有方法...
Thanks in advance for any ideas! 预先感谢您的任何想法!
So, I figured what was happening - I hadn't described the open() method as part of the @sftp_mock object, so @sftp.file.open looked like a private method. 因此,我知道发生了什么-我没有将open()方法描述为@sftp_mock对象的一部分,因此@ sftp.file.open看起来像一个私有方法。 Fixing this required having @sftp_mock.file return @sftp_mock, so that it could be chained to the open() method like this:
要解决此问题,需要使@ sftp_mock.file返回@sftp_mock,以便可以将其链接到open()方法,如下所示:
@sftp_mock.stub!(:file) { @sftp_mock }
@sftp_mock.should_receive(:open).with("some/pathname/filename").and_return(@file)
I was also missing the @file declaration...the complete spec file is: 我也缺少@file声明...完整的spec文件是:
require 'spec_helper'
describe "SftpToServer" do
let(:ftp) { BatchTask::SftpToServer.new }
it "should return a file stream" do
@file = File.new("#{RAILS_ROOT}/spec/files/express_checkout_tdr.csv", "r")
@sftp_mock = mock('sftp')
@entry = File.stubs(:reads).with("filename").returns(@file)
@entry_mock = mock('entry')
@entry_mock.stub(:name).and_return("filename")
@sftp_mock.stub_chain(:dir, :glob).and_yield(@entry_mock)
Net::SFTP.stub(:start).and_yield(@sftp_mock)
@sftp_mock.stub!(:file) { @sftp_mock }
@sftp_mock.should_receive(:open).with("some/pathname/filename").and_return(@file)
ftp.get_file_stream("ftp.test.com", "user", "password", "some/pathname", "filename").should be_kind_of(IO)
end
end
This other stackoverflow question helped me with this concept: Stub chain together with should_receive 这个另一个stackoverflow问题帮助了我这个概念: 存根链和should_receive一起
Also, feel free to comment if there's a better way to mock and stub out SFTP. 另外,如果有更好的模拟和存根SFTP的方法,请随时发表评论。 This spec example has gotten kinda ugly!
这个规范的例子有点丑陋!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.