Skip to content

Instantly share code, notes, and snippets.

@kaeff
Last active December 17, 2015 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaeff/5635833 to your computer and use it in GitHub Desktop.
Save kaeff/5635833 to your computer and use it in GitHub Desktop.
Spork, Guard Test Setup for Rails

Spork, Guard, RSpec Test Setup for Rails 3.1+

Running test automatically is great in theory, but the go-to solution when using RSpec based on Spork and Guard is mediocre to say the least. It forces you to remember all details of which component does what, who trigges what, what's reloaded at what stage and so on. Despite the fact that this was really confusing to start off with, maybe you're like me and never really cared to learn these details (and you really shouldn't need to!).

This is the setup I found working for me for plain Rails 3.1+ and RSpec.

Readings

guard 'rspec', cli: '--drb', all_on_start: false, all_after_pass: false 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{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_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('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# Capybara features specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end
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('Gemfile')
watch('Gemfile.lock')
watch('spec/spec_helper.rb') { :rspec }
watch('test/test_helper.rb') { :test_unit }
watch(%r{features/support/}) { :cucumber }
end
require 'rubygems'
require 'spork'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
# Trap autoloads to prevent class caches so that application parts are loaded on each run
require "rails/application"
Spork.trap_method(Rails::Application, :eager_load!)
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
# Require environment
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
RSpec.configure do |config|
# ...
end
end
Spork.each_run do
# This code will be run each time you run your specs.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment