Skip to content

Instantly share code, notes, and snippets.

@niku
Created December 17, 2010 13:35
Show Gist options
  • Save niku/744935 to your computer and use it in GitHub Desktop.
Save niku/744935 to your computer and use it in GitHub Desktop.
# -*- 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
# -*- 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