Skip to content

Instantly share code, notes, and snippets.

@santosh79
Created April 16, 2011 08:39
Show Gist options
  • Save santosh79/922982 to your computer and use it in GitHub Desktop.
Save santosh79/922982 to your computer and use it in GitHub Desktop.
A generic spec helper
require 'spork'
ENV["RAILS_ENV"] ||= 'test'
# Patch for RubyMine and Spork
ruby_mines = Dir["/Applications/RubyMine*.app"]
unless ruby_mines.empty?
$LOAD_PATH.unshift "#{ruby_mines.first}/rb/testing/patch/bdd/"
$LOAD_PATH.unshift "#{ruby_mines.first}/rb/testing/patch/common/"
end
Spork.prefork do
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
config.mock_with :mocha
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.use_instantiated_fixtures = false
config.global_fixtures = :all
config.include RspecTagMatchers
config.include Devise::TestHelpers, :type => :controller
config.include Requests::Authentication, :type => :request
# http://37signals.com/svn/posts/2742-the-road-to-faster-tests
disabled_gc = false
config.before(:all) do
# Hack for preventing GC disabling for request specs that don't respect :after calls
if described_class && !disabled_gc
disabled_gc = GC.disable
end
end
RESERVED_IVARS = %w(@loaded_fixtures)
config.after(:each) do
(instance_variables - RESERVED_IVARS).each do |ivar|
instance_variable_set ivar, nil
end
end
config.after(:all) do
if disabled_gc
GC.enable
disabled_gc = false
end
end
config.before(:each) do
Crypto.stubs(:decrypt).with(is_a(String)).returns('decrypted_string')
Crypto::DataSecure.stubs(:decrypt).with(is_a(String), is_a(String)).returns('decrypted_string')
end
end
if Spork.using_spork?
ActiveSupport::Dependencies.clear
ActiveRecord::Base.instantiate_observers
end
end
Spork.each_run do
if Spork.using_spork?
load 'Sporkfile' if File.exists? "Sporkfile"
end
# Reload Routes on each run
Cypress::Application.reload_routes!
# Reload factories each run
Factory.factories.clear
Dir[Rails.root.join("spec/factories/**/*.rb")].each { |f| load f }
end
# patch TestWarden to use configured failure app and correctly render response body returned from failure app
class Devise::TestHelpers::TestWarden
def catch_with_redirect(&block)
result = catch(:warden, &block)
if result.is_a?(Hash) && !custom_failure? && [email protected](:performed?)
result[:action] ||= :unauthenticated
env = @controller.request.env
env["PATH_INFO"] = "/#{result[:action]}"
env["warden.options"] = result
Warden::Manager._before_failure.each { |hook| hook.call(env, result) }
call_result = Devise.warden_config[:failure_app].call(env)
# status, headers, body = Devise::FailureApp.call(env).to_a
status, headers, response = call_result.to_a
@controller.send :render, :status => status, :text => response.body,
:content_type => headers["Content-Type"], :location => headers["Location"]
nil
else
result
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment