# Unit Testing : line by line?

**URL:** <https://www.ruby-forum.com/t/unit-testing-line-by-line/185733>\
**Category:** Ruby\
**Created:** [March 25, 2010, 10:37am UTC](https://www.ruby-forum.com/t/unit-testing-line-by-line/185733 "2010-03-25T10:37:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![onionwushu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5daacb/32.png) [@onionwushu](https://www.ruby-forum.com/u/onionwushu)\
**Post date:** [March 25, 2010, 10:37am UTC](https://www.ruby-forum.com/t/unit-testing-line-by-line/185733/1 "2010-03-25T10:37:10Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![onionwushu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5daacb/32.png) [@onionwushu](https://www.ruby-forum.com/u/onionwushu)\
**Post date:** [March 25, 2010, 10:56am UTC](https://www.ruby-forum.com/t/unit-testing-line-by-line/185733/2 "2010-03-25T10:56:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![onionwushu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5daacb/32.png) [@onionwushu](https://www.ruby-forum.com/u/onionwushu)\
**Post date:** [March 25, 2010, 11:37am UTC](https://www.ruby-forum.com/t/unit-testing-line-by-line/185733/3 "2010-03-25T11:37:39Z")

</div>

On Thu, Mar 25, 2010 at 10:37 AM, onion wushu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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 @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.

---

<div class="post-metadata">

**Author:** ![onionwushu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5daacb/32.png) [@onionwushu](https://www.ruby-forum.com/u/onionwushu)\
**Post date:** [March 26, 2010, 3:46am UTC](https://www.ruby-forum.com/t/unit-testing-line-by-line/185733/4 "2010-03-26T03:46:29Z")

</div>

On 3/25/10, onion wushu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 @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.)
