Thursday, August 13, 2015

Using RSpec with Ruby Gem

You can generate the required files when you create the directory structure for a gem by doing :

bundler gem --test=rspec my_gem

If you have already created the gem skeleton, it uses Minitest by default. You can make the following changes to switch to rspec:

1. Add :

spec.add_development_dependency "rspec"

to .gemspec file. Run bundle install.

2. Create Rakefile in the root of the gem:

require "bundler/gem_tasks"
require "rspec/core/rake_task"

task :default => :spec

3. Create spec/spec_helper.rb:

$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)

require 'your-gem'

4. Create spec/yukon_spec.rb:

require 'spec_helper'

describe 'Your Gem' do
  it 'has a version number' do
    expect(YourGem::VERSION).not_to be nil

  it 'does something useful' do
    expect(false).to eq(true)

5. Run it:

rspec spec/yukon_spec.rb --color --format doc