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.
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?
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: