I have a ruby extension that I'm building as a gem. It has this directory structure
|-ext
\-cowboy
\- extconf.rb
|- cowboy.c
|-lib
\- cowboy.rb
|- cowboy
\- version.rb
|- test
\- test_cowboy.rb
When I build it and install it, there are no errors, and 'require'ing the gem (e.g. require 'cowboy') works fine in irb.
However, when I run "ruby test/test_cowboy.rb" I get a load error from the require (it actually is complaining about "require 'cowboy/cowboy'" that lives in lib/cowboy.rb.
Does anyone know why???
I suspect that:
require 'cowboy'
, that tells rubygems to set up the load paths automatically to point to the currently installed gem dir.test/test_cowboy.rb
it doesn't require 'cowboy'
. This makes sense because during development, you don't want to load the installed version of the gem, which could be different from the code in your working dir.I think you should create a test/test_helper.rb
file that sets up the load path:
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
You may need to add other dirs if the compiled shared object file (.so
or .bundle
) isn't placed in lib
.
Then in each test file (e.g. test/test_cowboy.rb
), require test/test_helper.rb
:
require File.expand_path('../test_helper.rb', __FILE__)
You'll need to adjust that relative path if you have subdirs. E.g. if you have a file test/shoes/spur.rb
, you'd use:
require File.expand_path('../../test_helper.rb', __FILE__)