# "require" in unit test not returning true or false

**URL:** <https://www.ruby-forum.com/t/require-in-unit-test-not-returning-true-or-false/110080>\
**Category:** Ruby\
**Created:** [July 22, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/require-in-unit-test-not-returning-true-or-false/110080 "2007-07-22T20:26:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesper\_RSSSSnn-Jens](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/96bed5/32.png) [@Jesper\_RSSSSnn-Jens](https://www.ruby-forum.com/u/Jesper_RSSSSnn-Jens)\
**Post date:** [July 22, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/require-in-unit-test-not-returning-true-or-false/110080/1 "2007-07-22T20:26:48Z")

</div>

I have a test file in my project to test dependencies to ruby gems,  
arranged like this:

class DependenciesTest \< Test::Unit::TestCase

def test\_hpricot\_should\_exist  
actual = require ‘hpricot’  
assert\_equal(true, actual)  
end

… (more similar tests)

end # end class DependenciesTest

The documentation states that require should return true/false, but  
it does not — at least not for me in this test ☹

# Result:

1. Failure:  
test\_hpricot\_should\_exist(DependenciesTest) [test/unit/  
dependencies\_test.rb:19]:  
 expected but was  
\<[]\>.

My questions:

- Why doesn’t require return true?
- How should i form my dependencies test to make it work properly?

Thanks for any help and tips you can help me with!

/Jesper

---

<div class="post-metadata">

**Author:** ![Jesper\_RSSSSnn-Jens](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/96bed5/32.png) [@Jesper\_RSSSSnn-Jens](https://www.ruby-forum.com/u/Jesper_RSSSSnn-Jens)\
**Post date:** [July 22, 2007, 10:06pm UTC](https://www.ruby-forum.com/t/require-in-unit-test-not-returning-true-or-false/110080/2 "2007-07-22T22:06:34Z")

</div>

On Jul 22, 2007, at 11:24 , Jesper Rønn-Jensen wrote:

> I have a test file in my project to test dependencies to ruby gems,  
> arranged like this:
> 
> class DependenciesTest \< Test::Unit::TestCase
> 
> def test\_hpricot\_should\_exist  
> actual = require ‘hpricot’  
> assert\_equal(true, actual)  
> end

Look… I’m a testing _freak_ and even I don’t do this…

First off, you dunno if you’re using the REAL require or someone’s  
override.

Second, you’re not _actually_ testing require, so why bother?

If you want to have a quick and easy way to determine that a project  
has all the dependencies it needs, that seems better placed in the  
project’s rakefile or somesuch.

---

<div class="post-metadata">

**Author:** ![Jesper\_RSSSSnn-Jens](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/96bed5/32.png) [@Jesper\_RSSSSnn-Jens](https://www.ruby-forum.com/u/Jesper_RSSSSnn-Jens)\
**Post date:** [July 22, 2007, 10:32pm UTC](https://www.ruby-forum.com/t/require-in-unit-test-not-returning-true-or-false/110080/3 "2007-07-22T22:32:30Z")

</div>

On Jul 22, 10:05 pm, Ryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Look… I’m a testing _freak_ and even I don’t do this…
> 
> First off, you dunno if you’re using the REAL require or someone’s  
> override.
> 
> Second, you’re not _actually_ testing require, so why bother?
> 
> If you want to have a quick and easy way to determine that a project  
> has all the dependencies it needs, that seems better placed in the  
> project’s rakefile or somesuch.

Thanks Ryan, that will give me some leads to work on. Could you give  
an example on how you would do this in a project?

I’m not sure I follow you on how to do it with a rakefile…

One idea that comes to my mind is to new a class from the required  
library. That is actually testable and runs with no errors:

def test\_mechanize  
require ‘mechanize’  
m = WWW::Mechanize.new  
actual = m.class.to\_s  
assert\_equal(“WWW::Mechanize”, actual)  
end

Any comments on this? There is probably a better way…

/Jesper Rønn-Jensen  
[http://justaddwater.dk/](http://justaddwater.dk/)

---

<div class="post-metadata">

**Author:** ![Jesper\_RSSSSnn-Jens](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/96bed5/32.png) [@Jesper\_RSSSSnn-Jens](https://www.ruby-forum.com/u/Jesper_RSSSSnn-Jens)\
**Post date:** [July 23, 2007, 5:16am UTC](https://www.ruby-forum.com/t/require-in-unit-test-not-returning-true-or-false/110080/4 "2007-07-23T05:16:48Z")

</div>

> I’m not sure I follow you on how to do it with a rakefile…

task :check\_dependencies do  
{ :mechanize =\> “WWW::Mechanize”,  
:hpricot =\> “Hpricot”  
}.each do |dep, klass|  
begin  
require dep  
begin  
eval(“defined?(#{klass})”)  
rescue NameError  
abort("#{dep} was loaded but couldn’t find ‘#{klass}’")  
end  
rescue LoadError  
abort(“Couldn’t load ‘#{dep}’”)  
end  
end

^ manveru

---

<div class="post-metadata">

**Author:** ![Jesper\_RSSSSnn-Jens](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/96bed5/32.png) [@Jesper\_RSSSSnn-Jens](https://www.ruby-forum.com/u/Jesper_RSSSSnn-Jens)\
**Post date:** [July 23, 2007, 5:20am UTC](https://www.ruby-forum.com/t/require-in-unit-test-not-returning-true-or-false/110080/5 "2007-07-23T05:20:57Z")

</div>

# Should check my code before posting, sorry for wasting your bandwidth

🙂

require ‘rake’

task :check\_dependencies do  
{ ‘mechanize’ =\> “WWW::Mechanize”,  
‘hpricot’ =\> “Hpricot”  
}.each do |dep, klass|  
begin  
require dep  
abort("#{dep} was loaded but couldn’t find ‘#{klass}’") unless  
eval(“defined?(#{klass})”)  
rescue LoadError  
abort(“Couldn’t load ‘#{dep}’”)  
end  
end  
end

Rake::Task[:check\_dependencies].execute

---

<div class="post-metadata">

**Author:** ![Jesper\_RSSSSnn-Jens](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/96bed5/32.png) [@Jesper\_RSSSSnn-Jens](https://www.ruby-forum.com/u/Jesper_RSSSSnn-Jens)\
**Post date:** [July 23, 2007, 10:43pm UTC](https://www.ruby-forum.com/t/require-in-unit-test-not-returning-true-or-false/110080/6 "2007-07-23T22:43:10Z")

</div>

Michael.  
Thanks a lot for the example. It works perfectly for the two libraries  
you added.

I’m a bit uncertain as to which strategy to choose – the rake task or  
the unit test.  
Either way, it’s possible to make the test a prerequisite before  
running the actual application tests. So my intuition here tells me it  
doesn’t matter which solution to choose.

Any opinion on that?
