Created
August 3, 2010 15:58
-
-
Save ngty/506613 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Someone | |
class << self | |
def say | |
$stdin.each_line do |line| | |
$stdout << "someone says '#{line}' @ #{Time.now}" | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'rspec' | |
require 'otaku' | |
require 'cross-stub' | |
require File.join(File.dirname(__FILE__),'someone') | |
describe 'Someone' do | |
before do | |
# Cross-process stubbing if required (initializes the stubs cache) | |
CrossStub.setup :file => '/tmp/crosstub.cache' | |
# Wrap & execute stdin/stdout manipulation in another process (using otaku service) | |
Otaku.start do |data| | |
begin | |
# Need to explicitely require the definition for Someone (cos this block actually | |
# runs in another process) | |
require File.join(File.dirname(__FILE__),'someone') | |
# Replace stdout & stdin with instances of StringIO | |
$o_stdout, $o_stdin = $stdout, $stdin | |
$stdout, $stdin = StringIO.new, StringIO.new | |
$stdin << data | |
$stdin.rewind | |
# Cross-process stubbing if required (refreshes the stubs cache) | |
require 'cross-stub' | |
CrossStub.refresh :file => '/tmp/crosstub.cache' | |
# Running the to-be-tested method | |
Someone.say | |
# Grab the stdout dump | |
$stdout.rewind | |
$stdout.readlines.join | |
ensure | |
# Ensures stdout & stdin are reverted back to the originals | |
$stdout, $stdin = $o_stdout, $o_stdin | |
end | |
end | |
end | |
after do | |
Otaku.stop | |
CrossStub.clear | |
end | |
it 'should say something' do | |
Otaku.process('hello').should match(/^someone says 'hello'/) | |
end | |
it 'should say something else' do | |
Time.xstub(:now => '2012') | |
Otaku.process('hello').should == "someone says 'hello' @ 2012" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment