Unit Testing : line by line?

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 @[email protected]
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 !

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

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

why?

checking here

@b = “hello !”

another checking here

end
end

We could imagine that we would like to verify the properties @[email protected]
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.

On Thu, Mar 25, 2010 at 10:37 AM, onion wushu [email protected]
wrote:

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 @[email protected]
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.

On 3/25/10, onion wushu [email protected] wrote:

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 @[email protected]
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 @[email protected]
# 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.)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs