# Testing library code

**URL:** <https://www.ruby-forum.com/t/testing-library-code/111276>\
**Category:** Rails\
**Created:** [August 4, 2007, 9:12pm UTC](https://www.ruby-forum.com/t/testing-library-code/111276 "2007-08-04T21:12:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 4, 2007, 9:12pm UTC](https://www.ruby-forum.com/t/testing-library-code/111276/1 "2007-08-04T21:12:52Z")

</div>

Hi,

Just a quick question: If one creates custom classes and modules and  
places them in the lib/ folder of rails, where should the tests for  
those classes/modules go? Are they just unit tests?

Thanks,  
Toby

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 4, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/testing-library-code/111276/2 "2007-08-04T22:20:10Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hi,
> 
> Just a quick question: If one creates custom classes and modules and  
> places them in the lib/ folder of rails, where should the tests for  
> those classes/modules go? Are they just unit tests?
> 
> Thanks,  
> Toby

Well it is just ruby code so you can make a small ruby application and  
test it that way, and you can do it with test too I presume.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 4, 2007, 11:42pm UTC](https://www.ruby-forum.com/t/testing-library-code/111276/3 "2007-08-04T23:42:04Z")

</div>

What I mean is that I would like to use test::unit to test my modules  
and classes but I’m not sure where to put the test I write in the  
directory structure of rails. Some of my controllers rely on these  
modules/classes so they require testing, I’m just not sure how to go  
about it.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 5, 2007, 12:06am UTC](https://www.ruby-forum.com/t/testing-library-code/111276/4 "2007-08-05T00:06:58Z")

</div>

I tend to think of them as part of my unit tests  
(that is the way they generally get used)

If you want to put in a bit of work, you could very easily add a  
/test/lib directory  
and define a “test:libs” rake command.

namespace :test do  
Rake::TestTask.new(:libs =\> “db:test:prepare”) do |t|  
t.libs \<\< “test”  
t.pattern = ‘test/lib/\*\*/\*\_test.rb’  
t.verbose = true  
end  
Rake::Task[‘test:libs’].comment = “Run the lib tests in test/lib”  
end

stick that in a file in lib/tasks

you’d then want to overwrite the default test task, like so…

desc ‘Test all units and functionals’  
task :test do  
exceptions = [“test:units”, “test:libs”, “test:functionals”,  
“test:integration”].collect do |task|  
begin  
Rake::Task[task].invoke  
nil  
rescue =\> e  
e  
end  
end.compact

exceptions.each {|e| puts e;puts e.backtrace }  
raise “Test failures” unless exceptions.empty?  
end

(note: all of the code above is made from just small changes to the  
default tasks, found inside the rails gem –  
/rails/lib/tasks/testing.rake

removed\_email\_address@domain.invalid wrote:

> What I mean is that I would like to use test::unit to test my modules  
> and classes but I’m not sure where to put the test I write in the  
> directory structure of rails. Some of my controllers rely on these  
> modules/classes so they require testing, I’m just not sure how to go  
> about it.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 5, 2007, 12:30am UTC](https://www.ruby-forum.com/t/testing-library-code/111276/5 "2007-08-05T00:30:07Z")

</div>

removed\_email\_address@domain.invalid wrote:

> What I mean is that I would like to use test::unit to test my modules  
> and classes but I’m not sure where to put the test I write in the  
> directory structure of rails. Some of my controllers rely on these  
> modules/classes so they require testing, I’m just not sure how to go  
> about it.

Put all unit-level tests in test/unit.

The meaning of “unit test” isn’t “model test”; it’s generally the  
low-level  
tests.

If you don’t want to run too many tests between each edit, google for  
“test:svn\_modified” and use that to trigger only the tests files you  
changed  
since your last integration.

–  
Phlip  
[http://www.oreilly.com/catalog/9780596510657/](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax
