On 3/25/10, onion wushu <loic.lehenaff / 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.)