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

F

Feng Tien

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

hemant

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!

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.


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

7stud --

Feng said:
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.
 
R

Ryan Davis

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

Dale Martenson

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.

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
 

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

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top