Last Updated: February 25, 2016
·
8.997K
· job

Guard + Spork on Rails 4 with RSpec and Cucumber

Guard with Spork for RSpec, Cucumber and Test::Unit

It can be a bit of a pain if you want to use guard and spork, especially for the first time. There are many different gems available and not all work with every configuration.

We use this and it works without problems for us. If you use Mongoid, see the extra instructions at the end.

by JanDintel and JobV

Gems

group :development, :test do
  gem 'guard-rspec'
  gem 'guard-livereload'
  gem 'spork-rails', github: 'sporkrb/spork-rails' # rubygems version not rails 4 compatible
  gem 'guard-spork'
  gem 'childprocess'
end

group :test do
  gem 'rspec-rails'
  gem 'selenium-webdriver', '2.0.0'
  gem 'capybara', '2.1.0'
  gem 'factory_girl_rails'
  gem 'cucumber', '1.2.5' # Spork not supported as of Cucumber 1.3.0, need to use 1.2.5
  gem 'cucumber-rails', :require => false
  gem 'database_cleaner'
end

Install instruction

$ bundle update
$ bundle install
$ rails generate rspec:install
$ guard init rspec

Modify ./Guardfile

guard 'rspec', after_all_pass: false, cli: '--drb' do
...

Set up spork

$ bundle spork --bootstrap

Set up Spork in /spec/spec_helper.rb

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'
end

Setup guard with spork

$ guard init spork

Install Cucumber

$ rails generate cucumber:install --spork

Mongoid

For usage with mongoid, modify /spec/spec_helper.rb.
Remove these lines:

ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
..
config.fixture_path = "#{::Rails.root}/spec/fixtures"
..
config.use_transactional_fixtures = true

And add to /spec/spec_helper.rb, below Rspec.configure

config.before(:suite) do
  DatabaseCleaner[:mongoid].strategy = :truncation
end

config.before(:each) do
  DatabaseCleaner[:mongoid].start
end

config.after(:each) do
  DatabaseCleaner[:mongoid].clean
end

7 Responses
Add your response

Gem syntax error, you are missing the "do" from the first line.

over 1 year ago ·

Also noticed that the gem for rspec is missing. This might confuse people starting from afresh.

over 1 year ago ·

Thanks for the tips! Will change it right away :-)

over 1 year ago ·

Also, I think "bundle spork --bootstrap" should bve "bundle exec spork --bootstrap"

over 1 year ago ·

Thanks for the very informative and useful blog post. I did run into one problem. It seems that there's problems with zip/zip:

rake --tasks
rake aborted!
LoadError: cannot load such file -- zip/zip
/vagrant/config/application.rb:7:in <top (required)>' /vagrant/Rakefile:4:in<top (required)>'
(See full trace by running task with --trace)

I fixed this using this gem:

https://github.com/orien/zip-zip

Thanks again.

over 1 year ago ·

I've been chasing my tail all day trying to figure out the best way to implement these tools and speed up my tests... it's a total mess at this point :(

over 1 year ago ·

Nowadays it's best to just use Spring, which comes bundled with Rails 4.1 and up, rather than use guard. Spring is easier to use.

over 1 year ago ·