# Testing a plugin: Having to require plugin files manually?

**URL:** <https://www.ruby-forum.com/t/testing-a-plugin-having-to-require-plugin-files-manually/90239>\
**Category:** Rails\
**Created:** [March 26, 2007, 11:57pm UTC](https://www.ruby-forum.com/t/testing-a-plugin-having-to-require-plugin-files-manually/90239 "2007-03-26T23:57:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [March 26, 2007, 11:57pm UTC](https://www.ruby-forum.com/t/testing-a-plugin-having-to-require-plugin-files-manually/90239/1 "2007-03-26T23:57:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 16, 2007, 5:54am UTC](https://www.ruby-forum.com/t/testing-a-plugin-having-to-require-plugin-files-manually/90239/2 "2007-08-16T05:54:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 16, 2007, 12:57pm UTC](https://www.ruby-forum.com/t/testing-a-plugin-having-to-require-plugin-files-manually/90239/3 "2007-08-16T12:57:46Z")

</div>

On Aug 16, 4:54 am, Raymond O’Connor \<rails-mailing-l…@andreas-  
[s.net](http://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
