Skip to content

Instantly share code, notes, and snippets.

@sharnik
Created July 8, 2013 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharnik/5949668 to your computer and use it in GitHub Desktop.
Save sharnik/5949668 to your computer and use it in GitHub Desktop.
module MyFancyTool
module TestSupport
module ArubaExt
def with_redirected_stdout(&block)
redirect_stdout
yield
bring_back_stdout
end
def mock_stdout
unescape @stdout_cache
end
def mock_stderr
unescape @stderr_cache
end
def mock_output
mock_stdout + mock_stderr
end
private
def redirect_stdout
@stdout_cache = ''
@stderr_cache = ''
@stdout_redirected = true
@orig_stdout = $stdout
@orig_stderr = $stderr
$stdout = @mock_stdout = StringIO.new
$stderr = @mock_stderr = StringIO.new
end
def bring_back_stdout
@stdout_cache = @mock_stderr.string
@stderr_cache = @mock_stdout.string
@stdout_redirected = false
$stdout = @orig_stdout
$stderr = @orig_stderr
@orig_stdout = @mock_stdout = nil
@orig_stderr = @mock_stderr = nil
end
end
end
end
World(MyFancyTool::TestSupport::ArubaExt)
When /^I run my fancy tool with command "(.*)"$/ do |args|
args = args.split
args[0] = args[0].to_sym
Dir.chdir(current_dir) do
with_redirected_stdout do
MyFancyTool::MyCLI.start args
end
end
end
Then /^the last output should match (#{PATTERN})$/ do |expected|
assert { mock_output =~ expected }
end
Feature: Reassuring awesomeness of MyFancyTool.
Scenario: Running the tool
When I run my fancy tool with command "do stuff"
Then the last output should match /awesome output/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment