Skip to content

Instantly share code, notes, and snippets.

@elja
Created June 12, 2013 11:13
Show Gist options
  • Save elja/5764449 to your computer and use it in GitHub Desktop.
Save elja/5764449 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
prefork = lambda {
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
#require 'rspec/autorun'
require 'webmock/rspec'
require 'capybara/rspec'
require 'capybara/email/rspec'
require 'factory_girl'
require 'action_mailer'
require 'email_spec'
require 'pry'
require 'database_cleaner'
ActiveSupport::Dependencies.clear if Spork.using_spork?
WebMock.disable_net_connect!(allow_localhost: true, :allow => ["test.dev1.com",
"test.dev.com",
"dev.com",
"dev1.com",
# shipping method testing
"wwwcie.ups.com",
"gatewaybeta.fedex.com",
"production.shippingapis.com",
# payment method testing
"test.authorize.net",
"secure.authorize.net"
])
# 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}
Capybara.configure do |config|
config.ignore_hidden_elements = true
config.default_selector = :css
config.javascript_driver = :webkit
config.server_port = 3002
config.default_wait_time = 5
end
RSpec.configure do |config|
def set_host(host)
host! host
Capybara.app_host = "http://" + host
end
def test_sign_in(user, scope = :website_admin)
user.confirm!
controller.sign_in(scope, user)
end
config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
#config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include Devise::TestHelpers, :type => :controller
config.include Capybara::DSL, :type => :request
config.include Capybara::RSpecMatchers, :type => :request
# 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 = false
# Email test helper
config.include(EmailSpec::Helpers)
config.include(EmailSpec::Matchers)
# disable all observers
# see http://blog.terriblelabs.com/blog/2012/07/11/optional-observers-with-rails-3-and-rspec/
config.before do
ActiveRecord::Base.observers.disable :all
observers = example.metadata[:observer] || example.metadata[:observers]
if observers
ActiveRecord::Base.observers.enable *observers
end
end
config.treat_symbols_as_metadata_keys_with_true_values = true
config.before(:suite) do
#DatabaseCleaner.strategy = :truncation,
# { :except => %w[website_permissions permissions groups groups_permissions website_groups
# website_groups_permissions website_groups_sites clients sites website_site_infos] }
#DatabaseCleaner.clean_with(:truncation)
#load "#{Rails.root.to_s}/db/seeds.rb"
DatabaseCleaner.strategy = :transaction
end
config.before(:each) do
DatabaseCleaner.start
if example.metadata[:driver]
Capybara.current_driver = example.metadata[:driver] # or equivalent javascript driver you are using
else
Capybara.use_default_driver # presumed to be :rack_test
end
stub.instance_of(Site).choose_path do |security = true|
name = security ? "sites/private" : "sites/public"
create_test_file_storage(name)
end
stub.instance_of(Theme).secure_container do
create_test_file_storage("themes/private")
end
stub.instance_of(Theme).unsecure_container do
create_test_file_storage("themes/public")
end
end
config.after(:each) do
DatabaseCleaner.clean
# file for site delete automatically
["sites/private", "sites/public", "themes/private", "themes/public"].each do |name|
delete_test_file_storage(name)
end
end
Website::Category.any_instance.stubs(:valid?).returns(true)
config.include Capybara::DSL
end
}
each_run = lambda {
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
Rails.application.reload_routes!
FactoryGirl.reload
RSpec.configure do |config|
config.include CommonHelper
end
if Rails.env == "test" and Rails.configuration.database_configuration['test']['database'] == ':memory:' and
ActiveRecord::Base.connection.class == ActiveRecord::ConnectionAdapters::SQLiteAdapter ||
ActiveRecord::Base.connection.class == ActiveRecord::ConnectionAdapters::SQLite3Adapter
load "#{Rails.root.to_s}/db/schema.rb"
load "#{Rails.root.to_s}/db/seeds.rb"
end
}
if defined?(Zeus)
prefork.call
$each_run = each_run
class << Zeus.plan
def after_fork_with_test
after_fork_without_test
$each_run.call
end
alias_method_chain :after_fork, :test
end
elsif ENV['spork'] || $0 =~ /\bspork$/
require 'spork'
Spork.prefork(&prefork)
Spork.each_run(&each_run)
else
prefork.call
each_run.call
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment