Unit test an output with puts

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://DevLists.com. Sign up and save your mailbox.

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://DevLists.com. Sign up and save your mailbox.

--
Kevin Clark
http://glu.ttono.us

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.us/ZeekLand <-- NOT a blog!!!

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

···

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:

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

http://trackmap.robotcoop.com

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

···

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

--
Regards,
John Wilger

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

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.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

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

Yes.

···

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?

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

http://trackmap.robotcoop.com