# Test for Library Classes

**URL:** <https://www.ruby-forum.com/t/test-for-library-classes/131153>\
**Category:** Rails\
**Created:** [March 6, 2008, 2:41pm UTC](https://www.ruby-forum.com/t/test-for-library-classes/131153 "2008-03-06T14:41:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@Bala](https://www.ruby-forum.com/u/Bala)\
**Post date:** [March 6, 2008, 2:41pm UTC](https://www.ruby-forum.com/t/test-for-library-classes/131153/1 "2008-03-06T14:41:59Z")

</div>

Hi Experts,

Sorry if this questions makes you irritate. how do we write test for  
library classes?

for ex: i’ve class testing.rb in lib folder.

regards,  
Bala

---

<div class="post-metadata">

**Author:** ![Bala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@Bala](https://www.ruby-forum.com/u/Bala)\
**Post date:** [March 6, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/test-for-library-classes/131153/2 "2008-03-06T16:30:23Z")

</div>

Bala wrote:

> Sorry if this questions makes you irritate.

All questions make me irritate other people here. (-:

> how do we write test for  
> library classes?
> 
> for ex: i’ve class testing.rb in lib folder.

If you have a file lib/foo.rb, test it with test/unit/foo\_test.rb.

That’s why the folder is “test/unit”, not “test/model”.

And BTW use my awesome assert{ 2.0 } in your test cases, to make them  
even  
easier to write.

–  
Phlip  
[http://assert2.rubyforge.org/](http://assert2.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![Bala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@Bala](https://www.ruby-forum.com/u/Bala)\
**Post date:** [March 7, 2008, 6:12am UTC](https://www.ruby-forum.com/t/test-for-library-classes/131153/3 "2008-03-07T06:12:35Z")

</div>

Thanks Philip 🙂  
your assert looks good 🙂

---

<div class="post-metadata">

**Author:** ![Bala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@Bala](https://www.ruby-forum.com/u/Bala)\
**Post date:** [March 7, 2008, 8:05am UTC](https://www.ruby-forum.com/t/test-for-library-classes/131153/4 "2008-03-07T08:05:12Z")

</div>

Phlip,

I’ve a method like this, on lib/foo.rb class.

def import\_some\_data(opt = {})

```
if opt[:arg1] && opt[:arg2] && opt[:arg3]

  doc = parse_three_args(opt[:arg1], opt[:arg2], opt[:arg3])

elsif opt[:arg1] && opt[:arg2]

  doc = parse_two_arg(opt[:arg2])

elsif opt[:arg1]

  doc = parse_one_arg(opt[:arg1])

else

  doc = nil

end
parse_it(doc) if !doc.nil?
check_for_multiple_instance(opt[:multiple_instance], doc) if !

```

doc.nil?  
end

now is there way to write good test on this method? honeslty i’m so  
weak in testing. 🙂

regards,  
Bala

---

<div class="post-metadata">

**Author:** ![Bala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@Bala](https://www.ruby-forum.com/u/Bala)\
**Post date:** [March 7, 2008, 8:28am UTC](https://www.ruby-forum.com/t/test-for-library-classes/131153/5 "2008-03-07T08:28:05Z")

</div>

Bala wrote:

> ```
> doc = nil
> end
> parse_it(doc) if !doc.nil?
> check_for_multiple_instance(opt[:multiple_instance], doc) if !
> 
> ```
> 
> doc.nil?  
> end
> 
> now is there way to write good test on this method? honeslty i’m so  
> weak in testing. 🙂

Pretend the method did not exist yet, and write one test for one line in  
the method.

Suppose I pick the line parse\_one\_arg. I want to write a test that  
proves it did  
not exist yet. I would write this (renaming your function to  
importer()):

def test\_importer\_parses\_one\_arg  
doc = importer(:arg1 =\> ‘my data’)  
assert{ doc.get\_data == ‘my data’ }  
end

(Hit [http://assert2.rubyforge.org/](http://assert2.rubyforge.org/) to write simple but powerful assert{}  
statements like that! Otherwise, that’s the same as assert\_equal()…)

To pass that assertion, I must make importer() a little more “exposed”  
and  
“decoupled”. It must return the ‘doc’ that it created, and this ‘doc’  
must have  
a method to tells us what the parse did.

Here’s the importer() to pass that test:

def importer(opts)  
doc = parse\_one\_arg(opt[:arg1])  
return nil unless doc  
parse\_it(doc)  
check\_for\_multiple\_instance(opt[:multiple\_instance], doc)  
return doc  
end

The method does not do anything that the test didn’t ask for (except the  
nil-handling, which is always a good idea anyway). If you want more  
stuff in  
that function, you must write another test, first:

def test\_importer\_parses\_two\_args  
doc = importer(:arg1 =\> ‘my data’, :arg2 =\> ‘more data’)  
assert{ doc.get\_data == ‘my data, more data’ }  
end

Run that test, and make sure it fails for the correct reason. Make sure  
the  
assert prints out a diagnostic describing exactly what’s wrong. Only  
then should  
you upgrade importer():

def importer(opts)  
doc = if opts[:arg1] and opts[:arg2]  
parse\_two\_args(\*opts.values\_at(:arg1, :arg2))  
elsif opts[:arg1]  
parse\_one\_args(opts[:arg1])  
else  
nil  
end  
return nil unless doc  
parse\_it(doc)  
check\_for\_multiple\_instance(opt[:multiple\_instance], doc)  
return doc  
end

This process is called “test driven development”. Always run your tests  
after  
every few edits, always try to predict the result of the next run, and  
never  
write new code until you can get a test to fail for the right reason. If  
the  
tests fail, Undo or revert until they pass.

This process does not require you to be good at testing. You just  
pretend that  
you are very bad at programming - so bad that you must always have a  
test to  
show you the next thing to do.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![Bala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@Bala](https://www.ruby-forum.com/u/Bala)\
**Post date:** [March 7, 2008, 8:50am UTC](https://www.ruby-forum.com/t/test-for-library-classes/131153/6 "2008-03-07T08:50:25Z")

</div>

Thanks Phlip,

It really helped me, Thanks a lot.
