Skip to content

Instantly share code, notes, and snippets.

@brianjlandau
Created November 28, 2012 17:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save brianjlandau/4162654 to your computer and use it in GitHub Desktop.
Save brianjlandau/4162654 to your computer and use it in GitHub Desktop.
Setting up a Rails engine for MySQL, RSpec & Capybara

Setup RSpec and Capybara on a new Rails engine

  1. rails plugin new ENGINE_NAME --full --database=mysql

  2. Add to gemspec:

s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'shoulda-matchers'
s.add_development_dependency 'capybara'
s.add_development_dependency 'database_cleaner'
s.add_development_dependency 'factory_girl_rails', '~> 1.7'
  1. Modify gemspec test_files line to this:

     s.test_files = Dir["spec/**/*"]
    
  2. Modify Rakefile to look like this:

#!/usr/bin/env rake
begin
  require 'bundler/setup'
rescue LoadError
  puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end

APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
load 'rails/tasks/engine.rake'

Bundler::GemHelper.install_tasks

Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }

require 'rspec/core'
require 'rspec/core/rake_task'

desc "Run all specs in spec directory (excluding plugin specs)"
RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')

task :default => :spec
  1. mkdir spec

  2. mv test/dummy spec/

  3. rm -fR test

  4. Edit spec/spec_helper.rb:

$LOAD_PATH.unshift(File.dirname(__FILE__))
ENV['RAILS_ENV'] ||= 'test'

require File.expand_path("../dummy/config/environment.rb",  __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'shoulda-matchers'
require 'capybara/rails'
require 'capybara/rspec'
require 'database_cleaner'
require 'factory_girl_rails'

Rails.backtrace_cleaner.remove_silencers!

# Load support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }

DatabaseCleaner.strategy = :truncation

RSpec.configure do |config|
  config.mock_with :rspec
  config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = false

  config.after(:each, :type => :request) do
    DatabaseCleaner.clean       # Truncate the database
    Capybara.reset_sessions!    # Forget the (simulated) browser state
    Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver
  end
end
  1. Add this to your engine file:
config.generators do |g|
  g.test_framework      :rspec,        :fixture => false
  g.fixture_replacement :factory_girl, :dir => 'spec/factories'
  g.assets false
  g.helper false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment