I have a question with using Test::Unit::TestCase / RSpec (probably
applies to both I think). I would like to test the output to STDOUT from
the code that I’m testing as well as the results from it. Is there a
prefered way to go about this? Somehow directing STDOUT somewhere…?
On Jun 16, 2006, at 10:09 AM, Mike N. wrote:
I have a question with using Test::Unit::TestCase / RSpec (probably
applies to both I think). I would like to test the output to STDOUT
from
the code that I’m testing as well as the results from it. Is there a
prefered way to go about this? Somehow directing STDOUT somewhere…?
setting $stdio to a StringIO seems to be pretty productive.
-mat
Mat S. wrote:
setting $stdio to a StringIO seems to be pretty productive.
I assume you mean $stdout.
This is what I have thus far with $stdout. It seems to be capturing the
output, but I’m having trouble getting back the output from StringIO
later. Not sure what I’m missing. This is what I have.
$:.unshift File.join(File.dirname(FILE), ‘…’, ‘lib’)
require ‘dirs’
require ‘stringio’
context “In an empty directory” do
setup do
@testing_dir = File.join(File.dirname(FILE), ‘…’, ‘_test_temp’)
@test_dir = File.join(@testing_dir, ‘test_dir’)
Dir.mkdir @testing_dir
end
teardown do
Dir.rmdir @testing_dir
end
specify “we should be able to create any dir” do
puts “\n========================test first==================\n”
stdout_orig = $stdout
$stdout = StringIO.new
puts “\n========================test first end==================\n”
Dirs.create_dirs @test_dir
Dir[@test_dir].should_not_be_empty
Dir.rmdir @test_dir
puts "\n========================test ==================\n"
#$stdout.to_a.should_not_be_empty
stdout_orig.puts "testy", $stdout.readlines, "testy end"
$stdout.readlines.grep(/Creating/).should_not_be_empty
$stdout = stdout_orig
puts "\n========================test end ==================\n"
end
end
gives,
In an empty directory
========================test first==================
testy
testy end
- we should be able to create any dir (FAILED - 1)
ExpectationNotMetError in ‘In an empty directory we should be able to
create any dir’
[] should not be empty
/home/mike/projects/ruby/rspace/test/dirs_spec.rb:29:in `we should be
able to create any dir’
Finished in 0.001164 seconds
1 context, 1 specification, 1 failure
On Jun 16, 2006, at 10:05 AM, Mike N. wrote:
Mat S. wrote:
setting $stdio to a StringIO seems to be pretty productive.
I assume you mean $stdout.
This is what I have thus far with $stdout. It seems to be capturing
the
output, but I’m having trouble getting back the output from StringIO
later. Not sure what I’m missing. This is what I have.
Its hidden:
$ ri StringIO#string
-------------------------------------------------------- StringIO#string
strio.string → string
Returns underlying String object, the subject of IO.
–
Eric H. - [email protected] - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant
Eric H. wrote:
Its hidden:
$ ri StringIO#string
Thanks, that’s the ticket!
For posterity here’s what I got now. (I moved the $stdout stuff to the
setup and teardown sections so I can use it for the rest of my
specifies.)
$:.unshift File.join(File.dirname(FILE), ‘…’, ‘lib’)
require ‘dirs’
require ‘stringio’
context “In an empty directory” do
setup do
@testing_dir = File.join(File.dirname(FILE), ‘…’, ‘_test_temp’)
@test_dir = File.join(@testing_dir, ‘test_dir’)
Dir.mkdir @testing_dir
@stdout_orig = $stdout
$stdout = StringIO.new
end
teardown do
Dir.rmdir @testing_dir
#@stdout_orig.puts "testy", $stdout.string, "testy end"
$stdout = @stdout_orig
end
specify “we should be able to create any dir” do
Dirs.create_dirs @test_dir
Dir[@test_dir].should_not_be_empty
Dir.rmdir @test_dir
$stdout.string.grep(/Creating #{@test_dir}/).should_not_be_empty
end
end
looks pretty nice now, I love it. This gives,
$ spec -f s test/dirs_spec.rb
In an empty directory
- we should be able to create any dir
Finished in 0.000861 seconds
1 context, 1 specification, 0 failures