From: onion wushu on
Hi everybody !

I would like to verify some properties in a method, line by line. For
example:

class Hello
def initialize
@a = "hi !"
end

def say_hi
@b = "hi !"
# checking here
@b = "hello !"
# another checking here
end
end

We could imagine that we would like to verify the properties @a==@b
after the first declaration of @b and after the second one. Is there a
way to handle this case with TestUnit ? Any other suggestions ?

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

From: Ryan Davis on

On Mar 25, 2010, at 02:37 , onion wushu wrote:

> I would like to verify some properties in a method, line by line.

why?

> For
> example:
>
> class Hello
> def initialize
> @a = "hi !"
> end
>
> def say_hi
> @b = "hi !"
> # checking here
> @b = "hello !"
> # another checking here
> end
> end
>
> We could imagine that we would like to verify the properties @a==@b
> after the first declaration of @b and after the second one. Is there a
> way to handle this case with TestUnit ? Any other suggestions ?

No. and no.


From: Jesús Gabriel y Galán on
On Thu, Mar 25, 2010 at 10:37 AM, onion wushu <loic.lehenaff(a)gmail.com> wrote:
> Hi everybody !
>
> I would like to verify some properties in a method, line by line. For
> example:
>
> class Hello
>  def initialize
>    @a = "hi !"
>  end
>
>  def say_hi
>    @b = "hi !"
>    # checking here
>    @b = "hello !"
>    # another checking here
>  end
> end
>
> We could imagine that we would like to verify the properties @a==@b
> after the first declaration of @b and after the second one. Is there a
> way to handle this case with TestUnit ? Any other suggestions ?

The minimal unit for unit testing is a method. If you find the need to
unit test a part of the method that could be a sign that the method is
too complex and you should refactor it:

def say_hi
process_some_stuff("hi!")
process_other_stuff("hello!")
end

def process_some_stuff(value)
@b = value
end

def process_other_stuff(value)
@b = value
end

and you can unit test process_some_stuff and the other extracted
methods individually.

Jesus.

From: Caleb Clausen on
On 3/25/10, onion wushu <loic.lehenaff(a)gmail.com> wrote:
> Hi everybody !
>
> I would like to verify some properties in a method, line by line. For
> example:
>
> class Hello
> def initialize
> @a = "hi !"
> end
>
> def say_hi
> @b = "hi !"
> # checking here
> @b = "hello !"
> # another checking here
> end
> end
>
> We could imagine that we would like to verify the properties @a==@b
> after the first declaration of @b and after the second one. Is there a
> way to handle this case with TestUnit ? Any other suggestions ?

I think the straightforward way to do this kind of thing is to use
assertions. Not in a unit test, but right inline in your code. So,
say_hi should be written like this:

def say_hi
@b = "hi !"
assert @a==@b
# checking here
@b = "hello !"
# another checking here
end

How do you get an implementation of assert? There are a number of ruby
assert libraries out there; here are my 3 favorite alternatives:

1) from Test::Unit. include Test::Unit::Assertions in the class using assert
2) roll your own: a simple implementation is very short: def assert(x)
fail unless x end
3) the assert macro distributed with my gem rubymacros, which has some
pretty nice
error reporting capabilities for when one of your asserts pop. (And
you can strip all assertions when running in production.)

 | 
Pages: 1
Prev: Read & Write data on the web.
Next: Actuve Record