Skip to content

Instantly share code, notes, and snippets.

@ticktricktrack
Created June 6, 2012 11:34
Show Gist options
  • Select an option

  • Save ticktricktrack/2881400 to your computer and use it in GitHub Desktop.

Select an option

Save ticktricktrack/2881400 to your computer and use it in GitHub Desktop.
My crappy spec_helper with "performance tweaks" c&p'ed from various sources
# require 'simplecov'
# SimpleCov.start 'rails'
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require "cancan/matchers"
require 'database_cleaner'
# require 'perftools'
require 'resque_spec/scheduler'
require 'webmock/rspec'
##################################################################
# Stub the dynect api
module WebMock
# pass-through these domains (e.g. "www.facebook.com")
Allow = %w(www.incutio.com).freeze
end
# DynectEmailApi.send :include, DynectEmailApi::TestHelpers
# DynectEmailApi.reset!
###
##################################################################
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
ActionController::Base.class_eval do
def rescue_action(exception)
raise exception
end
end
# Do this to reduce IO during tests. I think it pretty much disables the logger
# Rails.logger.level = 4
# def build_fixtures
# puts 'build_fixtures 123'
# Fixtures.reset_cache
# fixtures_folder = File.join(Rails.root.to_s, 'spec', 'fixtures')
# fixtures = Dir[File.join(fixtures_folder, '*.yml')].map { |f| File.basename(f, '.yml') }
# DatabaseCleaner.clean
# Fixtures.create_fixtures(fixtures_folder, fixtures)
# end
def cp_is_proxied?
@cp_proxied ||= cp_current_user(true).proxy_target
end
def cp_current_user(force_absolute_current_user=false)
return current_user if force_absolute_current_user#if you must absolutely access the user that logged in
return current_user.proxy_target if current_user && current_user.proxy_target#if the logged-in user is proxying as another user then all methods of the site will be treated as the proxy target being the logged-in user
current_user
end
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
# config.run_all_when_everything_filtered = true
config.filter_run :focus => true
# config.filter_run_excluding :broken => true
Capybara.default_selector = :css
Capybara.javascript_driver = :webkit
Capybara.server_port = 61260
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.global_fixtures = :all
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
# 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
config.include Devise::TestHelpers, :type => :controller
config.include ControllerMacros
config.include LoginMacros
config.include RequestMacros, :type => :request
config.include MailerMacros
REDIS_PID = "#{Rails.root}/tmp/pids/redis-test.pid"
REDIS_CACHE_PATH = "#{Rails.root}/tmp/cache/"
config.before(:suite) do
# PerfTools::CpuProfiler.start("tmp/add_numbers_profile")
DatabaseCleaner.strategy = :truncation, {:except => %w[ ]}
DatabaseCleaner.clean_with(:truncation)
DatabaseCleaner.start
GC.disable
Fog.mock!
connection = Fog::Storage.new(Settings.fog)
connection.directories.create(key: Settings.bucket)
redis_options = {
"daemonize" => 'yes',
"pidfile" => REDIS_PID,
"port" => 9736,
"timeout" => 300,
"save 900" => 1,
"save 300" => 1,
"save 60" => 10000,
"dbfilename" => "dump.rdb",
"dir" => REDIS_CACHE_PATH,
"loglevel" => "debug",
"logfile" => "stdout",
"databases" => 16
}.map { |k, v| "#{k} #{v}" }.join('\n')
`echo '#{redis_options}' | redis-server -`
end
# # before each test that requires js, turn transactional fixtures off
# # build the fixtures and start the db cleaner
# config.before(js: true) do
# config.use_transactional_fixtures = false
# build_fixtures()
# DatabaseCleaner.start
# end
# # after each spec with js, turn transactional fixtures back on
# # rebuild fixtures
# config.after(:each, js: true) do
# config.use_transactional_fixtures = true
# build_fixtures()
# end
# speedup by disabling GC
RESERVED_IVARS = %w(@loaded_fixtures)
last_gc_run = Time.now
config.before(:each) do
GC.disable
WebMock.reset!
WebMock.disable_net_connect!(allow_localhost: true, allow: WebMock::Allow)
WebMock.stub_request(:any, "https://emailapi.dynect.net/rest/json/send").to_return(:status => 200, :body => "", :headers => {})
WebMock.stub_request(:any, "https://emailapi.dynect.net/rest/json/senders").to_return(:status => 200, :body => "", :headers => {})
WebMock.stub_request(:post, 'https://emailapi.dynect.net/rest/json/accounts').
to_return(:status => 200, body: "{\"response\":{\"status\":200,\"message\":\"OK\",\"data\":{\"apikey\":\"123456789abcde\"}}}",
headers: {"content_type" => ["application/json"]})
WebMock.stub_request(:post, "https://emailapi.dynect.net/rest/json/accounts/xheaders").to_return(:status => 200, :body => "", :headers => {})
DynectEmailApi.reset!
ResqueSpec.reset!
# Fog::Mock.reset
end
config.after(:each) do
(instance_variables - RESERVED_IVARS).each do |ivar|
instance_variable_set(ivar, nil)
end
if Time.now - last_gc_run > 1.0
GC.enable
GC.start
last_gc_run = Time.now
end
end
config.after(:suite) do
%x{
cat #{REDIS_PID} | xargs kill -QUIT
rm -f #{REDIS_CACHE_PATH}dump.rdb
}
DatabaseCleaner.clean
# PerfTools::CpuProfiler.stop
end
end
# got this part from http://blog.plataformatec.com.br/2011/12/three-tips-to-improve-the-performance-of-your-test-suite/
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || retrieve_connection
end
end
# Forces all threads to share the same connection. This works on
# Capybara because it starts the web server in a thread.
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment