Unit test an output with puts

E

Eric Boucher

Hi,

I want to be able to unit/test an output with the method 'puts'. Here's
a small example of what I'm trying to achieve:

#-------8<--------
class Foo
def output
puts "bar"
end
end
#-------8<--------

...and in my test

#-------8<--------
require 'unit/test'
...
def test_output
myFoo = Foo.new
assert_equal("bar", myFoo.output)
end
#-------8<--------

The problem is that the 'puts' method returns nil and I don't know how
to catch the output.

Thanks in advance.
 
K

Kevin Clark

Hi Eric,
I'd do this with a mock object. With Mocha I'd do something like this:

myFoo.expects:)puts).with("bar")

The idea is that you don't really want to capture the output (you know
puts works) you just want to make sure it's called properly.

You may need to define a fake puts method in Foo to make this work. So
in your test:

class Foo
def puts(*args)
end
end

class TestCase...
end
 
P

Phlip

Eric said:
I want to be able to unit/test an output with the method 'puts'. Here's
a small example of what I'm trying to achieve:

puts is a convenience function. It's really $stdout.write() etc.

So use fileHandle.write(), and then pass in a handle to an IO-stream, or a
temporary file, into your routine.

Yes, that means you pass more crap around. Unit testing powerfully decouples
your code, such that you no longer couple even with convenience objects like
$stdout.
 
E

Eric Hodel

I want to be able to unit/test an output with the method 'puts'.
Here's
a small example of what I'm trying to achieve:

Use util_capture from the ZenTest gem. You get two StringIO objects
back from calling util_capture that hold the contents of $stdout and
$stderr in the block:

require 'test/unit'
require 'rubygems'
require 'test/zentest_assertions'

class Foo
def output
puts "bar"
end
end

class TestFoo < Test::Unit::TestCase

def test_output
out, err = util_capture do
Foo.new.output
end

assert_equal "bar\n", out.string
assert_equal "", err.string
end
end
 
J

John Wilger

Use util_capture from the ZenTest gem. You get two StringIO objects
back from calling util_capture that hold the contents of $stdout and
$stderr in the block:
out, err = util_capture do
Foo.new.output
end

Nice. Does this also prevent the output from being dumped to the
console during the test?

--
Regards,
John Wilger
http://johnwilger.com

-----------
Alice came to a fork in the road. "Which road do I take?" she asked.
"Where do you want to go?" responded the Cheshire cat.
"I don't know," Alice answered.
"Then," said the cat, "it doesn't matter."
- Lewis Carrol, Alice in Wonderland
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,212
Messages
2,571,101
Members
47,695
Latest member
KayleneBee

Latest Threads

Top