"require" in unit test not returning true or false

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 :frowning:

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

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.

On Jul 22, 10:05 pm, Ryan D. [email protected] 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/

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

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

:slight_smile:

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

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?