Skip to content

Testing with Spork, Guard, and Test::Unit

December 16, 2011

Gemfile:

group :test do
 gem 'factory_girl_rails'
 gem 'mocha'
 gem 'capybara'
 gem 'spork', '~> 0.9.0.rc'
 gem 'spork-testunit'
 gem 'guard-spork'
 gem 'guard-test'
 gem 'rb-inotify', :require => false
 gem 'rb-fsevent', :require => false
 gem 'rb-fchange', :require => false
 gem 'growl_notify'
end

Run:

bundle install
spork --bootstrap
guard init spork
guard init test

Guardfile:

guard 'spork', :wait => 60, :test_unit_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('test/test_helper.rb') { :test_unit }
end

guard :test, :drb => true do
 watch(%r{^lib/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
 watch(%r{^test/.+_test\.rb$})
 watch('test/test_helper.rb') { "test" }
 # Rails example
 watch(%r{^app/models/(.+)\.rb$}) { |m| "test/unit/#{m[1]}_test.rb" }
 watch(%r{^app/controllers/(.+)\.rb$}) { |m| "test/functional/#{m[1]}_test.rb" }
 watch(%r{^app/views/.+\.rb$}) { "test/integration" }
 watch('app/controllers/application_controller.rb') { ["test/functional", "test/integration"] }
end

test/test_helper.rb:

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.

 ENV["RAILS_ENV"] = "test"
 require File.expand_path('../../config/environment', __FILE__)
 require 'rails/test_help'
 require 'mocha'

 # =====================
 # = Integration Tests =
 # =====================
 require "capybara/rails"
 module ActionController
  class IntegrationTest
   include Capybara::DSL
  end
 end

 # ===============
 # = Other Tests =
 # ===============
 class ActiveSupport::TestCase
  fixtures :all
 end
end
Spork.each_run do
 # This code will be run each time you run your specs.
end

If you are using Active Admin you are going to want to include a little patch in your each_run block to avoid getting an “undefined method: view_factory” error:

Spork.each_run do
  ActionView::Template.register_template_handler :arb, lambda { |template|
    "self.class.send :include, Arbre::Builder; @_helpers = self; self.extend ActiveAdmin::ViewHelpers; @__current_dom_element__ = Arbre::Context.new(assigns, self); begin; #{template.source}; end; current_dom_context"
  }
end

Run your tests:

guard
About these ads

From → testing

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: