Created
December 17, 2010 13:35
-
-
Save niku/744935 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
# -*- coding: utf-8 -*- | |
require 'rspec' | |
describe "stdout" do | |
def substitute_stdout | |
mock = double('stdout') | |
yield mock | |
$stdout = mock | |
end | |
after do | |
$stdout = STDOUT # すり替えた$stdoutを元に戻す | |
end | |
it "print 'hoge'" do | |
substitute_stdout { |stdout| | |
stdout.should_receive(:write).with("hoge") | |
} | |
print "hoge" | |
end | |
it "puts 'foo' and 'bar'" do | |
substitute_stdout { |stdout| | |
stdout.should_receive(:write).with("foo").ordered | |
stdout.should_receive(:write).with("\n").ordered | |
stdout.should_receive(:write).with("bar").ordered | |
stdout.should_receive(:write).with("\n").ordered | |
} | |
puts "foo" | |
puts "bar" | |
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
# -*- coding: utf-8 -*- | |
require 'rspec' | |
describe "stdout" do | |
it "print 'hoge'" do | |
mock = double("stdout") | |
mock.should_receive(:write).with("hoge") | |
$stdout = mock | |
print "hoge" | |
end | |
it "puts 'foo' and 'bar'" do | |
mock = double("stdout") | |
mock.should_receive(:write).with("\n") | |
mock.should_receive(:write).with("\n") | |
mock.should_receive(:write).with("foo") | |
mock.should_receive(:write).with("bar") | |
$stdout = mock | |
# test passed... | |
# how do I check order "foo", "\n", "bar", "\n" ? | |
puts "foo" | |
puts "bar" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment