# Testing and STDOUT

**URL:** <https://www.ruby-forum.com/t/testing-and-stdout/63606>\
**Category:** Ruby\
**Created:** [June 16, 2006, 4:09pm UTC](https://www.ruby-forum.com/t/testing-and-stdout/63606 "2006-06-16T16:09:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![miken700](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@miken700](https://www.ruby-forum.com/u/miken700)\
**Post date:** [June 16, 2006, 4:09pm UTC](https://www.ruby-forum.com/t/testing-and-stdout/63606/1 "2006-06-16T16:09:15Z")

</div>

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…?

---

<div class="post-metadata">

**Author:** ![miken700](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@miken700](https://www.ruby-forum.com/u/miken700)\
**Post date:** [June 16, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/testing-and-stdout/63606/2 "2006-06-16T16:21:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![miken700](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@miken700](https://www.ruby-forum.com/u/miken700)\
**Post date:** [June 16, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/testing-and-stdout/63606/3 "2006-06-16T19:05:02Z")

</div>

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)

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

---

<div class="post-metadata">

**Author:** ![miken700](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@miken700](https://www.ruby-forum.com/u/miken700)\
**Post date:** [June 16, 2006, 8:16pm UTC](https://www.ruby-forum.com/t/testing-and-stdout/63606/4 "2006-06-16T20:16:10Z")

</div>

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. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![miken700](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@miken700](https://www.ruby-forum.com/u/miken700)\
**Post date:** [June 17, 2006, 12:44am UTC](https://www.ruby-forum.com/t/testing-and-stdout/63606/5 "2006-06-17T00:44:34Z")

</div>

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
