spec_helper.РБ для тестирования RSpec, капибара, FactoryGirl, Ловилка и разработки


Пройдя через сотни зачастую противоречивых постов на тестирование тестирования RSpec, капибара, FactoryGirl, ложка-вилка, предохранитель и разработать без огурца, это spec_helper что "вроде" работает.

Я готова к его взрывать или не работать, как и предыдущие попытки.

Вот моей spec_helper.РБ

Вопросы:

  1. Есть ли что-то в нем может вызвать затруднение?
  2. Есть ли недостатки? Все, что должно быть сделано лучше?

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


5171
6
задан 13 декабря 2011 в 01:12 Источник Поделиться
Комментарии