Forum: Ruby "require" in unit test not returning true or false

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jesper Rønn-Jensen (Guest)
on 2007-07-22 22:26
(Received via mailing list)
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
Ryan D. (Guest)
on 2007-07-23 00:06
(Received via mailing list)
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.
Jesper Rønn-Jensen (Guest)
on 2007-07-23 00:32
(Received via mailing list)
On Jul 22, 10:05 pm, Ryan D. <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/
Michael F. (Guest)
on 2007-07-23 07:16
(Received via mailing list)
> 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
Michael F. (Guest)
on 2007-07-23 07:20
(Received via mailing list)
# 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
Jesper Rønn-Jensen (Guest)
on 2007-07-24 00:43
(Received via mailing list)
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?
This topic is locked and can not be replied to.