Configuring SSHKit in Automated Tests

When using Ruby’s SSHKit gem in your application, you might want to test code that uses it. Most likely, you don’t want to connect to real servers when automated tests are run. Luckily SSHKit includes a backend just for that, called Printer.

You can easily configure your test suite to use it:

SSHKit.configure do |kit|
  kit.backend = SSHKit::Backend::Printer
end

In a Rails application for example, simply add this to your test_helper.rb. Now all the execute statements will be printer to the standard output.

You can also implement your own SSHKit backend, based on the Printer class, if you need something more involved, like saving all the commands into a Thread.current variable and running assertions on them within your test.