[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Unit test an output with puts

Stefan Lang

9/8/2006 7:30:00 PM

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.
--
Posted with http://De.... Sign up and save your mailbox.

6 Answers

Kevin Clark

9/8/2006 7:37:00 PM

0

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

On 9/8/06, Eric Boucher <devlists-ruby-talk@devlists.com> wrote:
> 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.
> --
> Posted with http://De.... Sign up and save your mailbox.
>
>


--
Kevin Clark
http://gl...

Phlip

9/8/2006 10:03:00 PM

0

Eric Boucher wrote:

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

--
Phlip
http://www.greencheese.u... <-- NOT a blog!!!


Eric Hodel

9/8/2006 11:51:00 PM

0

On Sep 8, 2006, at 12:29 PM, Eric Boucher wrote:

> 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

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...



John Wilger

9/9/2006 7:38:00 PM

0

On 9/8/06, Eric Hodel <drbrain@segment7.net> wrote:
> 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://john...

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

Eric Hodel

9/10/2006 3:01:00 AM

0

On Sep 9, 2006, at 12:38 PM, John Wilger wrote:

> On 9/8/06, Eric Hodel <drbrain@segment7.net> wrote:
>> 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?

Yes.

--
Eric Hodel - drbrain@segment7.net - http://blog.se...
This implementation is HODEL-HASH-9600 compliant

http://trackmap.rob...



Stefan Lang

9/10/2006 3:24:00 PM

0

Thanks everybody and thanks to Eric, this is exactly what I needed.

On Saturday, September 09, 2006, at 8:51 AM, Eric Hodel wrote:
>On Sep 8, 2006, at 12:29 PM, Eric Boucher wrote:
>
>> 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
>
>--
>Eric Hodel - drbrain@segment7.net - http://blog.se...
>This implementation is HODEL-HASH-9600 compliant
>
>http://trackmap.rob...
>
>
>




--
Posted with http://De.... Sign up and save your mailbox.