RSpec:Stubbing SFTP

Sly*_*Sly 8 rspec ruby-on-rails stubbing net-sftp

我试图从一个对象中删除Net :: 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
Run Code Online (Sandbox Code Playgroud)

这是规格:

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).with("filename").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
Run Code Online (Sandbox Code Playgroud)

这是堆栈跟踪:

Spec::Mocks::MockExpectationError in 'SftpToServer should return a file stream'
Mock "entry" received :name with unexpected arguments
  expected: ("filename")
       got: (no args)
/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:
Run Code Online (Sandbox Code Playgroud)

首先,我的方法就在这里吗?我想删除SFTP的功能,因为我们可以确信它经过了很好的测试.相反,我想专注于确保get_file_stream()中的"黑盒子"返回文件流.

其次,我如何正确地存根sftp.file.open()来实现这一目标?

在此先感谢任何想法!

Sam*_*cey 11

首先嘲笑sftp是个好主意,原因有两个:

  1. 你没有编写sftp测试,专注于测试你打算测试的内容.
  2. 您在测试中删除了对网络的任何依赖性 - 您不希望测试因为您无法控制而开始失败.

就错误而言,这是您的直接问题:

@entry_mock.stub(:name).with("filename").and_return("filename")
Run Code Online (Sandbox Code Playgroud)

在这里你是顽固entry.name("filename")而不仅仅是entry.name.

将其更改为:

@entry_mock.stub(:name).and_return("filename")
Run Code Online (Sandbox Code Playgroud)

让我知道你是怎么过的.