"require" in unit test not returning true or false

Discussion in 'Ruby' started by Jesper Rønn-Jensen, Jul 22, 2007.

  1. 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
     
    Jesper Rønn-Jensen, Jul 22, 2007
    #1
    1. Advertising

  2. Jesper Rønn-Jensen

    Ryan Davis Guest

    On Jul 22, 2007, at 11:24 , Jesper R=F8nn-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 =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.
     
    Ryan Davis, Jul 22, 2007
    #2
    1. Advertising

  3. On Jul 22, 10:05 pm, Ryan Davis <> 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/
     
    Jesper Rønn-Jensen, Jul 22, 2007
    #3
  4. > 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 Fellinger, Jul 23, 2007
    #4
  5. # 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
     
    Michael Fellinger, Jul 23, 2007
    #5
  6. 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?
     
    Jesper Rønn-Jensen, Jul 23, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran

    Does true ^ true return false?

    Siemel Naran, Jun 17, 2004, in forum: C++
    Replies:
    19
    Views:
    709
    Chris Theis
    Jun 18, 2004
  2. Pierre Quentel

    "0 in [True,False]" returns True

    Pierre Quentel, Dec 12, 2005, in forum: Python
    Replies:
    59
    Views:
    1,087
    Grant Edwards
    Dec 16, 2005
  3. André
    Replies:
    3
    Views:
    1,679
  4. bdb112
    Replies:
    45
    Views:
    1,427
    jazbees
    Apr 29, 2009
  5. timr
    Replies:
    2
    Views:
    182
Loading...

Share This Page