Skip to content

Instantly share code, notes, and snippets.

@NullVoxPopuli
Created November 12, 2014 22:09
Show Gist options
  • Save NullVoxPopuli/99de77e5eca65eeb4864 to your computer and use it in GitHub Desktop.
Save NullVoxPopuli/99de77e5eca65eeb4864 to your computer and use it in GitHub Desktop.
# how to configure automated testing:
# http://www.rubyinside.com/how-to-rails-3-and-rspec-2-4336.html
##################################################################33
require 'spork'
if ENV['COVERAGE']
require 'simplecov'
SimpleCov.start 'rails' do
add_filter "/vendor/"
add_group 'Changed' do |source_file|
`git ls-files --exclude-standard --others \
&& git diff --name-only \
&& git diff --name-only --cached`.split("\n").detect do |filename|
source_file.filename.ends_with?(filename)
end
end
end
end
def sqlite?
Rails.configuration.database_configuration["test"]["adapter"] == "sqlite3"
end
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
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.
# This file is copied to ~/spec when you run 'ruby script/generate rspec'
# from the project root directory.
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path(File.join(File.dirname(__FILE__),'..','config','environment'))
# require 'spec/autorun'
require 'rspec/rails'
# We have mixed the usage of mocha and rspec mocks.
# We've told rspec to use :mocha below which makes the standalone require necessary.
require 'rspec/mocks/standalone'
require "mocha/api"
#require supporting gems
# require "database_cleaner"
require "factory_girl"
# formsack test support
require "formstack/rspec"
# first init will load factories, this should only run on subsequent reloads
FactoryGirl.factories.clear
FactoryGirl.sequences.clear
FactoryGirl.traits.clear
FactoryGirl.find_definitions
# Uncomment the next line to use webrat's matchers
#require 'webrat/integrations/rspec-rails'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
RSpec.configure do |config|
ap "Adapter: #{Rails.configuration.database_configuration['test']['adapter']}"
ap "Database: #{Rails.configuration.database_configuration['test']['database']}"
#########################
# General RSpec Configuration
#########################
config.include Devise::TestHelpers, :type => :controller
config.include ValidUserRequestHelper, :type => :request
# Use color in STDOUT
config.color_enabled = true
# Use color not only in STDOUT but also in pagers and files
config.tty = true
#config.use_transactional_fixtures = true # default was true
config.use_instantiated_fixtures = true
# clean database
config.before :suite do
if sqlite?
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean_with :truncation
config.use_transactional_fixtures = false
else
DatabaseCleaner.strategy = :transaction
end
end
config.before(:each) do |x|
Time.zone = "UTC"
DatabaseCleaner.start
ActionMailer::Base.deliveries.clear
# when running a spec file, un comment this to see which 'F's belong
# to which tests
# ap "#{x.class.description} #{x.example.description}"
AWS.stub!
end
config.after(:each) do
DatabaseCleaner.clean
$rollout.unstub(:active?)
end
config.before(:all) do
# reduce db hits
PublicActivity.enabled = false
end
config.after(:all) do
DatabaseCleaner.clean
end
##########################
# Factory Girl Syntax
##########################
#https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md
config.include FactoryGirl::Syntax::Methods
##########################
# integration tests
##########################
Capybara.ignore_hidden_elements = false
Capybara.default_wait_time = 8
# connects to phantomjs
require 'capybara/rspec'
# Capybara.default_driver = :webkit
# Capybara.javascript_driver = :webkit
require 'capybara/poltergeist'
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {debug: false, js_errors: false, timeout: 120})
end
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
# == Fixtures
#
# You can declare fixtures for each example_group like this:
# describe "...." do
# fixtures :table_a, :table_b
#
# Alternatively, if you prefer to declare them only once, you can
# do so right here. Just uncomment the next line and replace the fixture
# names with your fixtures.
#
# config.global_fixtures = :table_a, :table_b
# config.global_fixtures = :all
#
# If you declare global fixtures, be aware that they will be declared
# for all of your examples, even those that don't use them.
#
# You can also declare which fixtures to use (for example fixtures for test/fixtures):
#
# config.fixture_path = Rails.root + '/spec/fixtures/'
#config.fixture_path = Rails.root + '/spec/fixtures/'
#
# == Mock Framework
#
# RSpec uses its own mocking framework by default. If you prefer to
# use mocha, flexmock or RR, uncomment the appropriate line:
# #
config.mock_with :mocha
# == Notes
#
# For more information take a look at Spec::Runner::Configuration and Spec::Runner
#
#
# RSpec-retry
config.verbose_retry = true # show retry status in spec process
retry_count = ENV['RSPEC_RETRY_COUNT']
config.default_retry_count = retry_count.try(:to_i) || 0
puts "RSpec retry count is #{config.default_retry_count}"
end
end
Spork.each_run do
# runs each time the suite is ran
ActiveRecord::Schema.verbose = false
load "#{Rails.root.to_s}/db/schema.rb" # use db agnostic schema by default
end
if not sqlite?
# http://stackoverflow.com/questions/8774227/why-not-use-shared-activerecord-connections-for-rspec-selenium
unless ENV['PARALLEL_TEST_GROUPS'].present?
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || ConnectionPool::Wrapper.new(:size => 1) { retrieve_connection }
end
end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
end
ActiveRecord::Schema.verbose = false
load "#{Rails.root.to_s}/db/schema.rb" # use db agnostic schema by default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment