"require" in unit test not returning true or false

  • Thread starter Jesper Rønn-Jensen
  • Start date
J

Jesper Rønn-Jensen

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

2) Failure:
test_hpricot_should_exist(DependenciesTest) [test/unit/
dependencies_test.rb:19]:
<true> 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
 
R

Ryan Davis

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 =3D 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 =20
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 =20
has all the dependencies it needs, that seems better placed in the =20
project's rakefile or somesuch.
 
J

Jesper Rønn-Jensen

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/
 
M

Michael Fellinger

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
 
M

Michael Fellinger

# 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
 
J

Jesper Rønn-Jensen

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top