Skip to content

Instantly share code, notes, and snippets.

@bnorton
Created April 30, 2012 15:11
Show Gist options
  • Save bnorton/2559113 to your computer and use it in GitHub Desktop.
Save bnorton/2559113 to your computer and use it in GitHub Desktop.
RSpec, Capybara, DatabaseCleaner

Rails Application With RSpec, Capybara, Jasmine, DatabaseCleaner

Database Cleaner is configured to use transactions for non-request tests for speed but uses database truncation on request (acceptance) tests for compatibility with capybara's use of a separate ruby thread to run tests in.

source 'http://rubygems.org'
gem 'rails'
gem 'ejs'
gem 'i18n-js', :git => 'git://github.com/fnando/i18n-js.git'
gem 'jquery-rails'
gem 'airbrake'
gem 'thin'
group :assets do
gem 'sass-rails', '~> 3.2.4'
gem 'uglifier', '>= 1.0.3'
end
group :development, :test do
gem 'rspec-rails'
gem 'jasmine', :git => 'git://github.com/pivotal/jasmine-gem.git'
end
group :test do
gem 'capybara'
gem 'webmock'
gem 'timecop'
gem 'factory_girl_rails'
gem 'database_cleaner'
gem 'headless'
end
module RequestHelper
extend ActiveSupport::Concern
included do
metadata[:js] = true
end
def sign_in(user)
visit "/users/sign_in"
fill_in I18n.t("sessions.new.email"), :with => user.email
fill_in I18n.t("sessions.new.password"), :with => user.password
click_button I18n.t("sessions.new.sign_in")
end
def reload_page
visit current_path
end
def within_header
within "#header" do
yield
end
end
def within_overlay
within "#overlay" do
yield
end
end
def expect_path(path)
wait_until { current_path == path }
current_path.should == path
rescue Capybara::TimeoutError
flunk "Waiting for current path to equal #{path}, Timed out."
end
end
ENV["RAILS_ENV"] ||= 'test'
require_relative "../config/environment"
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'rspec/expectations'
require 'webmock/rspec'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.use_transactional_fixtures = false
config.infer_base_class_for_anonymous_controllers = false
# define modules in spec/support and include it for all :type tests
config.include RequestHelper, :type => :request
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with :truncation
end
config.before(:each) do
if example.metadata[:js]
DatabaseCleaner.strategy = :truncation
else
DatabaseCleaner.start
end
load Rails.root.join('db', 'seeds.rb')
WebMock.disable_net_connect!(:allow_localhost => true)
end
config.after(:each) do
WebMock.allow_net_connect!
Timecop.return
DatabaseCleaner.clean
if example.metadata[:js]
DatabaseCleaner.strategy = :transaction
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment