Unit Testing, how to use assert_equal for last printed line?

I've been learning Ruby the last couple weeks and trying to figure how
to Unit Test

I wrote a method that outputs a few lines of text.

How do I write an assertion so it tests if, say the last line printed is
equal to the expected? Lasts just say the program asks for 2 numbers,
and outputs all the number from the lowest to the highest.

I tried using:

assert_equal '11' , fizzbang(10,100)

but that does not work, because puts methods outputs are nil.

how do you match puts? is there a different assertion you have to use?

or is there no way to do this with Test::Unit assertion, will I have to
extract the text somehow from the method? How do I do that?

Thanks!

···

--
Posted via http://www.ruby-forum.com/.

The idea about Unit Testing is, you do not write tests to test
libraries, but you write tests to test your code. So, your idea of
having assert_equal on puts is flawed from beginning. If you want to
test how many numbers was in your output, you are probably better off
saving that output in some variable and running asserts against the
variable.

While Unit Testing, its very important to understand, what you want to
test? It comes with a little practise. Libraries like RSpec help in
that.

···

On 10/24/07, Feng Tien <pood.forums@gmail.com> wrote:

I've been learning Ruby the last couple weeks and trying to figure how
to Unit Test

I wrote a method that outputs a few lines of text.

How do I write an assertion so it tests if, say the last line printed is
equal to the expected? Lasts just say the program asks for 2 numbers,
and outputs all the number from the lowest to the highest.

I tried using:

assert_equal '11' , fizzbang(10,100)

but that does not work, because puts methods outputs are nil.

how do you match puts? is there a different assertion you have to use?

or is there no way to do this with Test::Unit assertion, will I have to
extract the text somehow from the method? How do I do that?

Thanks!

--
Let them talk of their oriental summer climes of everlasting
conservatories; give me the privilege of making my own summer with my
own coals.

http://gnufied.org

Feng Tien wrote:

I've been learning Ruby the last couple weeks and trying to figure how
to Unit Test

Generally, you use assertions to test the return values of methods.
Some people use "test driven design", which means they write the tests
first, then they write code that complements the tests, i.e. code that
is easily testable. So, for instance in your case, after writing your
test, you might write a method that returns the values to be output, and
then you would write some other code to output the values. Your unit
test would test the return values from the method. However, you would
not have any idea whether the code that outputs the values does so in
the desired format.

In my limited experience with unit testing, you can't easily unit test
methods that gather user input or methods that output info to the user.
For instance, if a method prompts the user for info, how can you unit
test that method without actually entering input by hand. If you think
about it for a little awhile, the solution involves replacing your data
gathering method with a method that just returns lots of different
values. There are theories about how to do that in the best way, and
they use things called 'mock objects', which you can google.

···

--
Posted via http://www.ruby-forum.com/.

The easiest way to do it is to store off stdout and replace it with a StringIO and test against that. Replace stdout in teardown.

···

On Oct 24, 2007, at 01:30 , Feng Tien wrote:

How do I write an assertion so it tests if, say the last line printed is
equal to the expected? Lasts just say the program asks for 2 numbers,
and outputs all the number from the lowest to the highest.

Simple example:

require 'test/unit'
require 'stringio'

class Blah
  def do_something
    puts "Done"
  end
end

class TestBlah < Test::Unit::TestCase
  def setup
    @save_stdout = $stdout
    $stdout = StringIO.new
  end

  def teardown
    $stdout = @save_stdout
  end

  def test_do_something
    b = Blah.new
    b.do_something
    assert_equal "Done\n", $stdout.string
  end
end

···

On Oct 24, 8:42 am, Ryan Davis <ryand-r...@zenspider.com> wrote:

On Oct 24, 2007, at 01:30 , Feng Tien wrote:

> How do I write an assertion so it tests if, say the last line
> printed is
> equal to the expected? Lasts just say the program asks for 2 numbers,
> and outputs all the number from the lowest to the highest.

The easiest way to do it is to store off stdout and replace it with a
StringIO and test against that. Replace stdout in teardown.