Skip to content

Instantly share code, notes, and snippets.

@chrisbloom7
Forked from oliverbarnes/env.rb
Created December 23, 2011 14:48
Show Gist options
  • Save chrisbloom7/1514388 to your computer and use it in GitHub Desktop.
Save chrisbloom7/1514388 to your computer and use it in GitHub Desktop.
Cucumber setup with spork and database_cleaner
<%
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
%>
default: <%= std_opts %> features
wip: --drb -tags @wip:3 --wip features
autotest: --drb --color --format progress --strict
rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= "test"
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
require 'cucumber/rails'
# We're going to use our custom factory_girl step definition file from
# https://gist.github.com/1513688#file_factory_girl_steps.rb
# require 'factory_girl/step_definitions'
# Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
# order to ease the transition to Capybara we set the default here. If you'd
# prefer to use XPath just remove this line and adjust any selectors in your
# steps to use the XPath syntax.
Capybara.default_selector = :css
# By default, any exception happening in your Rails application will bubble up
# to Cucumber so that your scenario will fail. This is a different from how
# your application behaves in the production environment, where an error page will
# be rendered instead.
#
# Sometimes we want to override this default behaviour and allow Rails to rescue
# exceptions and display an error page (just like when the app is running in production).
# Typical scenarios where you want to do this is when you test your error pages.
# There are two ways to allow Rails to rescue exceptions:
#
# 1) Tag your scenario (or feature) with @allow-rescue
#
# 2) Set the value below to true. Beware that doing this globally is not
# recommended as it will mask a lot of errors for you!
#
ActionController::Base.allow_rescue = false
# Remove/comment out the lines below if your app doesn't have a database.
# For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
begin
require 'database_cleaner'
require 'database_cleaner/cucumber'
DatabaseCleaner.strategy = :truncation
rescue NameError
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
# You may also want to configure DatabaseCleaner to use different strategies for certain features and scenarios.
# See the DatabaseCleaner documentation for details. Example:
#
# Before('@no-txn,@selenium,@culerity,@celerity,@javascript') do
# DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
# end
#
# Before('~@no-txn', '~@selenium', '~@culerity', '~@celerity', '~@javascript') do
# DatabaseCleaner.strategy = :transaction
# end
#
# Possible values are :truncation and :transaction
# The :transaction strategy is faster, but might give you threading problems.
# See https://github.com/cucumber/cucumber-rails/blob/master/features/choose_javascript_database_strategy.feature
Cucumber::Rails::Database.javascript_strategy = :truncation
end
Spork.each_run do
# No need to put your hooks here or even require the hooks.rb or local.rb
end
Before do
# This seems to cause more problems then it solves
# DatabaseCleaner.start
# Load in any common data that will be used by the application Make sure you use
# load() rather than require() since required files will only be loaded once
load("#{Rails.root}/db/seeds.rb")
# You can add any factory generators here that you want re-gen'd on each scenario
# Factory.create(:user)
# Factory.create(:admin)
end
After do |scenario|
# This seems to cause more problems then it solves
# DatabaseCleaner.clean
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment