It is pretty easy with the added generator of rspec-rails to setup RSpec for testing a Rails application. But how about adding RSpec for testing a gem in development?
I am not using jeweler or such tools. I just used Bundler (bundle gem my_gem) to setup the structure for the new gem and edit the *.gemspec manually.
I also added s.add_development_dependency "rspec", ">= 2.0.0" to gemspec and did a bundle install.

4 Answers
4

Bundler supports gem development perfectly. If you are creating a gem, the only thing you need to have in your Gemfile is the following:

source "https://rubygems.org"
gemspec

This tells Bundler to look inside your gemspec file for the dependencies when you run bundle install.

Next up, make sure that RSpec is a development dependency of your gem. Edit the gemspec so it reads:

spec.add_development_dependency "rspec"

Next, create spec/spec_helper.rb and add something like:

require 'bundler/setup'
Bundler.setup
require 'your_gem_name' # and any other gems you need
RSpec.configure do |config|
# some (optional) config here
end

The first two lines tell Bundler to load only the gems inside your gemspec. When you install your own gem on your own machine, this will force your specs to use your current code, not the version you have installed seperately.

Create a spec, for example spec/foobar_spec.rb:

require 'spec_helper'
describe Foobar do
pending "write it"
end

Optional: add a .rspec file for default options and put it in your gem's root path:

To be fair, you should instead invoke RSpec's init command to generate the spec skeleton files rather than having to manually type them in. This would ensure compatibility with the version of RSpec that you are using: rspec --init
–
Attila GyörffyMar 27 '12 at 12:50

7

rspec --init wasn't available when I wrote this, but good point!
–
iainMar 30 '12 at 19:50

Actually I found the best way to do the requires in the spec helper is this: require 'rubygems' require 'bundler/setup' Bundler.require(:default, :development)
–
mkonNov 8 '13 at 4:30

Make a dir in your gem's root called spec, put your specs in there. You probably already have rspec installed, but if you don't, just do a gem install rspec and forget Gemfiles and bundler.

Next, you'll make a spec, and you need to tell it where your app is, where your files are, and include the file you want to test (along with any dependencies it has):

# spec/awesome_gem/awesome.rb
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
$: << File.join(APP_ROOT, 'lib/awesome_gem') # so rspec knows where your file could be
require 'some_file_in_the_above_dir' # this loads the class you want to test
describe AwesomeGem::Awesome do
before do
@dog = AwesomeGem::Awesome.new(name: 'woofer!')
end
it 'should have a name' do
@dog.name.should eq 'woofer!'
end
context '#lick_things' do
it 'should return the dog\'s name in a string' do
@dog.lick_things.should include 'woofer!:'
end
end
end

If you want some .rspec options love, go make a .rspec file and put it in your gem's root path. Mine looks like this:

# .rspec
--format documentation --color --debug --fail-fast

Easy, fast, neat!

I like this because you don't have to add any dependencies to your project at all, and the whole thing remains very fast. bundle exec slows things down a little, which is what you'd have to do to make sure you're using the same version of rspec all the time. That 0.56 seconds it took to run two tests was 99% taken up by the time it took my computer to load up rspec. Running hundreds of specs should be extremely fast. The only issue you could run into that I'm aware of is if you change versions of rspec and the new version isn't backwards compatible with some function you used in your test, you might have to re-write some tests.

This is nice if you are doing one-off specs or have some good reason to NOT include rspec in your gemspec, however it's not very good for enabling sharing or enforcing compatibility.