19.07.2009
Testing hoe gem projects with rspec
Since version 1.9 hoe contains a rake task spec to test your gem with rspec. This task is available only if you create a folder spec in your gem project:
# somegem/spec/somegem_spec.rb
require File.join(File.dirname(__FILE__), 'spec_helper')
describe "somegem" do
it "should have tests" do
pending "write tests or I will kneecap you"
end
end
The specs don’t see your lib classes by default, so it’s recommended to add a spec helper for this:
# somegem/spec/spec_helper.rb
SPEC_DIR = File.dirname(__FILE__)
lib_path = File.expand_path("#{SPEC_DIR}/../lib")
$LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
require 'somegem'
To add options like colored output and diff comparisions, add a own spec:spec task and use a spec.opts file:
# somegem/Rakefile
# ...
namespace :spec do
desc "Run all specs"
Spec::Rake::SpecTask.new('spec') do |t|
t.spec_files = FileList['spec/**/*_spec.rb']
t.spec_opts = ['--options', 'spec/spec.opts']
end
end
Example for somegem/spec/spec.opts:
--colour
--format
profile
--timeout
20
--diff
Have a look at rspec itself for a full featured example of testing hoe projects with rspec (rspec itself is built using hoe): http://github.com/dchelimsky/rspec/

