[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

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

Feng Tien

10/24/2007 5:31:00 AM

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

6 Answers

hemant

10/24/2007 6:46:00 AM

0

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!

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://g...

7stud --

10/24/2007 7:01:00 AM

0

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

Ryan Davis

10/24/2007 1:43:00 PM

0


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.


Dale Martenson

10/24/2007 4:24:00 PM

0

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.

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

Hert

4/19/2011 1:06:00 PM

0

Its not surprising that LHL wants to get rid of Ah Goh's supporters.
After all the old man can go anytime; after which LHL will have to stand on
his own two feet.


<decoy404@hotmail.com> wrote in message
news:de052801-2c80-4bd4-b584-77e29029a040@j35g2000prb.googlegroups.com...
On Apr 11, 9:19 pm, "truth" <tr...@universe.com> wrote:
> Lim Boon Heng was surprised that he has been dropped when LHL told him.
> Just
> two days before his surprised announcement that he is retiring, he not
> only
> did not show any sign of retiring but even hinted that he could be used as
> an ACE card to counter any serious challenge from the oppositions.
> So Lim Boon Heng was axed as a reprisal for GCT's talk of restricting
> ministers to two terms which was first rebuffed by WKS ( a Lee loyalist)
> and
> then subsequently confirmed by LHL himself. All is not well in the pap
> camp.
> LHL is trying hard to stamp his authority on the party. It is a signal to
> the liberals to stay in line. Lately the liberals and hardliners are
> giving
> a mixed and confusing signals. LHL is taking the opportunity to weed out
> Goh
> loyalists and line the party with Lee loyalists.
> The pap is heading for a major defeat. They can easily lose their 2/3
> majority. Singaporeans have wakened up and are no longer afraid of the LKY
> and his pap gangsters.

So, he cried about the money he would no longer be getting.


Hert

4/19/2011 1:07:00 PM

0

Don't prey prey ha.
ministers get 1 mill. OK.

"AleXX" <dalaibaru@bishop_aka_tweety.net> wrote in message
news:io74tj$ior$1@registered.motzarella.org...
>A "minister without poforlio" is just like a minister, putting on long
>sleeve shirt with a tie on, well polished shoe with only his underpant on
>attending meetings in pariahment? What type of minister is he..!!!
>
> Take a look:
> http://www.sgpolitics.n...
>
>
> <decoy404@hotmail.com> wrote in message
> news:de052801-2c80-4bd4-b584-77e29029a040@j35g2000prb.googlegroups.com...
> On Apr 11, 9:19 pm, "truth" <tr...@universe.com> wrote:
>> Lim Boon Heng was surprised that he has been dropped when LHL told him.
>> Just
>> two days before his surprised announcement that he is retiring, he not
>> only
>> did not show any sign of retiring but even hinted that he could be used
>> as
>> an ACE card to counter any serious challenge from the oppositions.
>> So Lim Boon Heng was axed as a reprisal for GCT's talk of restricting
>> ministers to two terms which was first rebuffed by WKS ( a Lee loyalist)
>> and
>> then subsequently confirmed by LHL himself. All is not well in the pap
>> camp.
>> LHL is trying hard to stamp his authority on the party. It is a signal to
>> the liberals to stay in line. Lately the liberals and hardliners are
>> giving
>> a mixed and confusing signals. LHL is taking the opportunity to weed out
>> Goh
>> loyalists and line the party with Lee loyalists.
>> The pap is heading for a major defeat. They can easily lose their 2/3
>> majority. Singaporeans have wakened up and are no longer afraid of the
>> LKY
>> and his pap gangsters.
>
> So, he cried about the money he would no longer be getting.
>