[rspec-users] Mocking Net::SSH connections

Jamie D jam5t3r.lists at gmail.com
Mon Apr 21 02:03:21 EDT 2008


Hi,

So I'm stuck again with creating a mock for Net::SSH, I've managed to
mock the call to Net::SSH.start and yield the Net::SSH mock but I am
totally stuck with mocking the session.shell.sync call and will also
need to mock the shell.send_command call also. Any help much
appreciated.

When I run the following spec I get the error:
Mock 'Net::SSH' received unexpected message :shell with (no args)

I've tried things like @shell = mock(Net::SSH::Service::Shell) but
this also gives an error complaining that Service doe snot exist.

# test code
    @connection = Ssh::Remote.new
    @ssh = mock(Net::SSH)
    Net::SSH.should_receive(:start).and_yield(@ssh)
    Net::SSH.should_receive(:shell).and_return('something')

# library code
require 'net/ssh'

module Ssh
  class Remote

    def remote_command(server, user, commands=[])
      Net::SSH.start(server, user) do |session|
        shell = session.shell.sync
        commands.each do |command|
          out = shell.send_command command[:command]
        end
      end
    end
  end
end


More information about the rspec-users mailing list