Skip to content

Instantly share code, notes, and snippets.

@jdjkelly
Last active November 26, 2017 03:07
Show Gist options
  • Save jdjkelly/5200375 to your computer and use it in GitHub Desktop.
Save jdjkelly/5200375 to your computer and use it in GitHub Desktop.
Rails 3 – Setting Up Guard with Rspec and Spork with Growl Notifications in OSX

Gemfile:

group :development do
  gem 'rspec-rails'
  gem 'guard'
  gem 'guard-rspec'
end

group :test do
  gem 'rspec'
  gem 'growl'
  gem 'rb-fsevent'
  gem 'spork-rails'
  gem 'guard-spork'
end


bundle install

Now download Mac OSX Growl here. Double click on the downloaded dmg file and install Growl. Relaunch the dmg file and click on the Extras folder, then click on growlnotify, and install it using the pkg file.

Now, assuming that you have Rspec tests in place, type this in the terminal to initiate a Guardfile with the Guard for Rspec:

bundle exec guard init rspec

This will generate a Guardfile in your app directory. Now we will fill it with code to activate the Spork Guard. Type in the terminal:

bundle exec guard init spork

Now according to the guard-spork Github page, the Spork guard code must always come before the RSpec code in the Guardfile, so edit your Guardfile to look like this:

# A sample Guardfile
# More info at https://github.com/guard/guard#readme
 
guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do
  watch('config/application.rb')
  watch('config/environment.rb')
  watch(%r{^config/environments/.+\.rb$})
  watch(%r{^config/initializers/.+\.rb$})
  watch('spec/spec_helper.rb')
end
 
guard 'rspec', :version => 2 do
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
  watch('spec/spec_helper.rb')  { "spec" }
 
  # Rails example
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^lib/(.+)\.rb$})                           { |m| "spec/lib/#{m[1]}_spec.rb" }
  watch(%r{^app/controllers/(.+)_(controller)\.rb$})  { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
  watch(%r{^spec/support/(.+)\.rb$})                  { "spec" }
  watch('spec/spec_helper.rb')                        { "spec" }
  watch('config/routes.rb')                           { "spec/routing" }
  watch('app/controllers/application_controller.rb')  { "spec/controllers" }
  # Capybara request specs
  watch(%r{^app/views/(.+)/.*\.(erb|haml)$})          { |m| "spec/requests/#{m[1]}_spec.rb" }
end

Now we will modify the spec_helper.rb file in the spec/ folder to setup Spork. For further explanations, loo here. The file should contain the following code:

require 'rubygems'
require 'spork'
 
Spork.prefork do
  ENV['RAILS_ENV'] ||= 'test'
  require File.expand_path('../../config/environment', __FILE__)
  require 'rspec/rails'
 
  Dir[Rails.root.join('spec/support/**/*.rb')].each{|f| require f}
 
  RSpec.configure do |config|
    config.mock_with :rspec
    config.fixture_path = "#{Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = true
 
    ActiveSupport::Dependencies.clear
  end
end
 
Spork.each_run do
  load "#{Rails.root}/config/routes.rb"
  Dir["#{Rails.root}/app/**/*.rb"].each {|f| load f}
end

As you can see, the difference is that we put the previous code inside the Spork.prefork block and added a configuration for the Spork.each_run block.

Now start testing by typing in the terminal:

bundle exec guard start

If you encounter an error, you can send me an Send me an email or write a comment when I get it up and running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment