Ruby Flare

Your awesome Tagline

3 notes

Yielding multiple times

When you need to stub out the values you pass into a block you can use rspec’s and_yield method:

sftp_mock.stub_chain(:dir, :foreach).and_yield(entry_1)

When you need to pass in values several times you can chain the and_yield:

sftp_mock.stub_chain(:dir, :foreach).and_yield(entry_1).and_yield(entry_2)

These examples were for spec’ing the behaviour of downloading files from an sftp server where in my tests I’ve mocked out the sftp server (entry_1 and entry_2 are mock files). The code is something like this:

Net::SFTP.start('host', 'username', :password => 'password', :auth_methods => ['password']) do |sftp|
  sftp.dir.foreach('/results') do |entry|
    # do stuff
  end
end

In my tests, I mocked out the sftp with the following:

sftp_mock = mock('sftp')
Net::SFTP.stub(:start).and_yield(sftp_mock)

Filed under rspec sftp

  1. rubyflare posted this