Ruby on rails RSpec:stubing SFTP-调用私有方法

Ruby on rails RSpec:stubing SFTP-调用私有方法,ruby-on-rails,rspec,mocking,sftp,Ruby On Rails,Rspec,Mocking,Sftp,我试图在模型中剔除SFTP。模型如下: 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_stre

我试图在模型中剔除SFTP。模型如下:

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
以下是规格:

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
这是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:
“SftpToServer应返回文件流”中的NoMethodError 调用了私有方法“open”# /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:'get_file_stream'中 ./spec/models/batch_task/sftp_to_server_spec.rb:15: 我在谷歌上搜索过这个,但我不明白为什么RSpec会将sftp.file.open视为私有方法


提前感谢您的任何想法

所以,我知道发生了什么-我没有将open()方法描述为@sftp_mock对象的一部分,所以@sftp.file.open看起来像一个私有方法。修复此问题需要使用@sftp_mock.file return@sftp_mock,以便将其链接到open()方法,如下所示:

@sftp_mock.stub!(:file) { @sftp_mock }
@sftp_mock.should_receive(:open).with("some/pathname/filename").and_return(@file)
我还缺少@file声明…完整的规范文件是:

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
另一个问题帮助我理解了这个概念:


此外,如果有更好的方法来模拟和剔除SFTP,请随时发表评论。这个规范示例有点难看

所以,我知道发生了什么-我没有将open()方法描述为@sftp_mock对象的一部分,所以@sftp.file.open看起来像一个私有方法。修复此问题需要使用@sftp_mock.file return@sftp_mock,以便将其链接到open()方法,如下所示:

@sftp_mock.stub!(:file) { @sftp_mock }
@sftp_mock.should_receive(:open).with("some/pathname/filename").and_return(@file)
我还缺少@file声明…完整的规范文件是:

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
另一个问题帮助我理解了这个概念:

此外,如果有更好的方法来模拟和剔除SFTP,请随时发表评论。这个规范示例有点难看