Testing a plugin: Having to require plugin files manually?

Hi all

I have written the following small plugin:

init.rb

require File.dirname(FILE) + ‘/lib/hash’

lib/hash.rb

class Hash

def except(*keys)
self.reject { |k,v|
keys.include? k.to_sym
}
end

def only(*keys)
self.dup.reject { |k,v|
!keys.include? k.to_sym
}
end
end

Then I wanted to add a test:

test/hash_test.rb

require ‘test/unit’

class HashTest < Test::Unit::TestCase
def test_hash_should_respond_to_except
assert({}.respond_to?(:except))
end

def test_hash_should_respond_to_only
assert({}.respond_to?(:only))
end
end

When running it from the plugin directory I get the following errors:

Loaded suite
/usr/lib/ruby/gems/1.8/gems/rake-0.7.2/lib/rake/rake_test_loader
Started
FF
Finished in 0.034015 seconds.

  1. Failure:
    test_hash_should_respond_to_except(HashTest) [./test/hash_test.rb:5]:
    is not true.

  2. Failure:
    test_hash_should_respond_to_only(HashTest) [./test/hash_test.rb:9]:
    is not true.

2 tests, 2 assertions, 2 failures, 0 errors
rake aborted!
Command failed with status (1): [/usr/bin/ruby -Ilib:lib
"/usr/lib/ruby/gem…]

(See full trace by running task with --trace)

This seems like the plugins files are not automatically required by
the testing framework. Do I have to require them myself within the test
file?

Thanks
Josh

Im curious about this too. If you do have to require them manually, is
there an easy way to do it? I’ve tried requiring just the init.rb file
and it doesn’t seem to do the trick

On Aug 16, 4:54 am, Raymond O’Connor <rails-mailing-l…@andreas-
s.net> wrote:

Im curious about this too. If you do have to require them manually, is
there an easy way to do it? I’ve tried requiring just the init.rb file
and it doesn’t seem to do the trick

Posted viahttp://www.ruby-forum.com/.

Plugins are normally loaded in the test environment, so there’s
something more subtle going on here.

To help understand what’s going on, it might be worth your while
adding a “puts 'loading my hash plugin” at the top of your init.rb,
and noting whether or not (or indeed, when) it gets loaded.

Additionally, how are you running the plugin tests? Via “rake
test:plugins”? However you are doing it, you need to be sure that the
Rails environment is being loaded first. Try adding the following line
to your plugin’s “test/hash_test.rb” file:

require File.join(File.dirname(FILE), “…”, “…”, “…”, “test”,
“test_helper”)

I might’ve got the number of “…”‘s wrong there, but basically the
idea is to load Rails’ test/test_helper.rb file. This will then ensure
that the Rails environment is loaded, and your plugin will get loaded
as you’d expect.

HTH,


James