Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Having been through hundreds of often-conflicting posts on testing with RSpec, Capybara, FactoryGirl, Spork, Guard and Devise without Cucumber, this is the spec_helper which "seems to" work.

I'm bracing for it to blow up or not work like the previous attempts.

Here's my spec_helper.rb

Questions:

  1. Is there something in it likely to cause trouble?
  2. Are there inefficiencies? Anything which ought to be done better?

require 'rubygems'
require 'spork'

def start_simplecov
  require 'simplecov'
  SimpleCov.start 'rails' unless ENV["SKIP_COV"]
end

def spork?
  defined?(Spork) && Spork.using_spork?
end

def simplecov?
  defined?(SimpleCov) && SimpleCov.using_simplecov?
end


def setup_environment

  # This file is copied to spec/ when you run 'rails generate rspec:install'
  ENV["RAILS_ENV"] ||= 'test'

  start_simplecov unless (spork? || !simplecov?)

  if spork?
    ENV['DRB'] = 'true'
#    require "rails/mongoid"
#    Spork.trap_class_method(Rails::Mongoid, :load_models)
    require "rails/application"
    Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
  end

  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
#  require 'rspec/autorun'

  require 'capybara/rails'
  require 'capybara/rspec'
  Capybara.default_host = 'http://localhost'
  Capybara.server_port = 3000
  include Capybara::DSL

  require 'factory_girl_rails'
 # FactoryGirl.factories.clear
#  FactoryGirl.find_definitions

  Rails.backtrace_cleaner.remove_silencers!

  require 'database_cleaner'

#  DatabaseCleaner.orm = "mongoid"
  DatabaseCleaner.strategy = :truncation

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

  RSpec.configure do |config|

    config.mock_with :rspec

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
    # config.fixture_path = "#{::Rails.root}/spec/fixtures"

    # If you're not using ActiveRecord, or you'd prefer not to run each of your
    # examples within a transaction, remove the following line or assign false
    # instead of true.
    config.use_transactional_fixtures = true

    config.before(:suite) do
      DatabaseCleaner.clean_with(:truncation)
    end

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

#    config.include Mongoid::Matchers
    config.include Devise::TestHelpers, :type => :controller
    config.extend ControllerMacros, :type => :controller
    config.include FactoryGirl::Syntax::Methods
    Capybara.javascript_driver = :rack_test

    # If true, the base class of anonymous controllers will be inferred
    # automatically. This will be the default behavior in future versions of
    # rspec-rails.
    config.infer_base_class_for_anonymous_controllers = false
  end
end

def each_run
  if spork?
#    FactoryGirl.definition_file_paths = [File.join(Rails.root, 'spec', 'factories')]
#    FactoryGirl.find_definitions
#    FactoryGirl.reload

    Pollsrv::Application.reload_routes!
    FactoryGirl.factories.clear
    Dir.glob("#{::Rails.root}/spec/factories/*.rb").each { |file| load "#{file}" }
  end

  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
end

# If spork is available in the Gemfile it'll be used but we don't force it.
unless (begin; require 'spork'; rescue LoadError; nil end).nil?
  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.
    setup_environment
    ActiveSupport::Dependencies.clear
#    Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
  end

  Spork.each_run do
    # This code will be run each time you run your specs.
    each_run
  end
else
#  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
  setup_environment
  each_run
end
share|improve this question

migrated from stackoverflow.com Dec 13 '11 at 23:08

This question came from our site for professional and enthusiast programmers.

1 Answer 1

A lot of this seems unnecessary. Many portions of this are done in the background, by the various libraries. Here's what mine looks like:

https://gist.github.com/1409074#file_spec_helper.rb

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.