RSpec: Stubbing SFTP
Asked Answered
C

1

10

I'm attempting to stub out Net::SFTP from an object. 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).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

Here's the stacktrace:

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:

First of all, is my approach right here? I want to remove the functionality of SFTP since we can be confident that it's pretty well tested. Instead, I want to focus on ensuring that a file stream is returned by the "black box" inside get_file_stream().

Second, how can I properly stub out sftp.file.open() to accomplish this?

Thanks in advance for any ideas here!

Crystalcrystalline answered 31/7, 2012 at 0:40 Comment(0)
R
12

Firstly mocking out sftp is a good idea for two reasons:

  1. You're not writing an sftp test, focus on testing just what you set out to test.
  2. You remove any dependency on the network in your tests - you don't want tests to start failing because of something out of your control.

As far as the error goes, this is your immediate problem:

@entry_mock.stub(:name).with("filename").and_return("filename")

Here you're stubbing entry.name("filename") rather than just entry.name.

Change it to:

@entry_mock.stub(:name).and_return("filename")

and let me know how you get on.

Rachmaninoff answered 31/7, 2012 at 3:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.