Last Updated: February 25, 2016
·
1.539K
· Eduardo Martines

Rails 3 and Rspec 2: Smooth and super fast

Gemfile

# code omitted

group :development, :test do
  gem "faker"
  gem "populator"
end

group :test do
  gem "capybara"
  gem "database_cleaner"
  gem "email_spec"
  gem "factory_girl_rails"
  gem "fuubar"
  gem "guard-spork"
  gem "guard-rspec"
  gem "growl"
  gem "rb-fsevent"
  gem "launchy"
  gem "rspec-rails"
  gem "spork-rails"
end

Guardfile

#!/usr/bin/env ruby
require "active_support/core_ext/string"

interactor :off

guard "spork", rspec_env: { RAILS_ENV: "test" } do
  watch("config/application.rb")
  watch("config/environment.rb")
  watch("config/environments/test.rb")
  watch(%r{^config/initializers/.+\.rb$})
  watch("Gemfile")
  watch("Gemfile.lock")
  watch("spec/spec_helper.rb")
  watch(%r{^spec/support/.+\.rb$})
end

guard "rspec", version: 2, cli: "--color --format Fuubar --fail-fast --drb" do
  watch(%r{^spec/.+_spec\.rb$})
  watch("config/routes.rb")                          { "spec/routing" }
  watch("app/controllers/application_controller.rb") { "spec/controllers" }
  watch(%r{^app/models/(.+)\.rb$})                   { |m| ["spec/controllers/#{m[1].pluralize}_controller_spec.rb", "spec/models/#{m[1]}_spec.rb", "spec/factories/#{m[1]}.rb"] }
  watch(%r{^spec/factories/(.+)\.rb$})               { |m| ["spec/controllers/#{m[1].pluralize}_controller_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"] }
end

spec/spec_helper.rb

Spork.prefork do
  ENV["RAILS_ENV"] ||= "test"

  require File.expand_path("../../config/environment", __FILE__)
  require "rspec/rails"
  require "capybara/rspec"
  require "capybara/rails"
  require "email_spec"
  require "sidekiq/testing"

  Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

  RSpec.configure do |config|
    # includes
    include ActionDispatch::TestProcess

    # rspec includes
    config.include(EmailSpec::Helpers)
    config.include(EmailSpec::Matchers)
    config.include(Devise::TestHelpers, type: :controller)

    # extends
    config.extend(ControllerMacros, type: :controller)
    config.extend(UploadMacros)

    # hooks
    config.before(:suite) do
      DatabaseCleaner.strategy = :transaction
      DatabaseCleaner.clean_with(:truncation)
    end

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end

    config.after(:all) do
      UploadMacros::cleanup if Rails.env.test?
    end
  end
end

Spork.each_run do
  Dir[Rails.root.join("app/models/*.rb")].each { |f| require f }
  FactoryGirl.reload
end

spec/support/controller_macros.rb

module ControllerMacros
  def login_admin
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:admin]
      sign_in FactoryGirl.create(:user, :with_group_admin)
    end
  end

  def login_user
    before(:each) do
      @request.env["devise.mapping"] = Devise.mappings[:user]
      user = FactoryGirl.create(:user, :with_groups)
      sign_in user
    end
  end
end

spec/support/upload_macros.rb

module UploadMacros
  def self.upload(opts)
    fixture_file_upload("#{Rails.root}/spec/files/#{opts[:file]}", opts[:type])
  end

  def self.cleanup
    FileUtils.rm_rf("#{Rails.root}/public/uploads/test")
  end
end

now just run and enjoy :)

guard -c