jxrhag
Last Updated: February 25, 2016
·
8.378K
· job
Bc8cd27a964b28bc06d5ad8020822b1c

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
Say Thanks
Respond

7 Responses
Add your response

10023

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

over 1 year ago ·
10024

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

over 1 year ago ·
10041
Bc8cd27a964b28bc06d5ad8020822b1c

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

over 1 year ago ·
11783
39a1ee78a1b69f724b4c64e0d3be52e9

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

over 1 year ago ·
13792
0 gyquotxda7gadoelxzzxoalfuaqfsgslxohxorqnpihotjs5asxbkk zc8naasudyx95lbnlms77

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 ·
18214
None

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 ·
18215
Bc8cd27a964b28bc06d5ad8020822b1c

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 ·
Awesome Job

A3e45f3a cade 11e7 83bc 71c9546a2749
Systems Engineer
·
Fulton, Maryland or Remote
·
Full Time