technicalpickles/capistrano-spec — pull request #6 by jgraichen (2013-04-07 14:06:35+00:00)
Title: Stub commands and their output to test capture calls.

I've add an option to stub commands and specify their output to test `capture` calls.

Example:
```ruby
# capistrano helper method
def remote_ruby_version
capture('ruby -v || true') =~ /(\d+\.\d+\.\d+p\d+)/ ? $1 : nil
end

#specs
it 'should return ruby version' do
@configuration.stub_command 'ruby -v || true', data: 'ruby 2.0.0p0 (2013-02-24...)'
@configuration.remote_ruby_version.should == '2.0.0p0'
end
```

As a stub invokes the block given to `run` also `sudo` or `invoke_command` with custom blocks can be stubbed by specifying the exact command. `channel` and `stream` block arguments can be given same way as `data` e.g. for testing error `:err` stream handling.

```ruby
# capistrano helper method
def installed_packages
sudo 'aptitude search ~i' do |channel, stream, data|
# magical foo
end
end

#specs
it 'should do something on error' do
@configuration.stub_command "sudo -p 'sudo password: ' aptitude search ~i",
stream: :err, data: 'command not found', channel: nil
# ...
end
```

I've also added specs for stubbing commands in a standalone spec file but I've needed to add capistrano as a test dependency.