Skip to content

Instantly share code, notes, and snippets.

@lenny
Created November 20, 2010 18:32
Show Gist options
  • Save lenny/708036 to your computer and use it in GitHub Desktop.
Save lenny/708036 to your computer and use it in GitHub Desktop.
spec helper for inclusion by specs that don't need/depend on the entire rails environment. In JRuby, specs that don't load entire rails env run significantly faster.
unless self.class.const_defined?('APP_ROOT')
APP_ROOT = File.expand_path(File.dirname(__FILE__) + '/../../../../../')
end
puts "#{File.basename(__FILE__)}: APP_ROOT: #{APP_ROOT}"
require File.expand_path("#{File.dirname(__FILE__)}/no_rails_env_spec_helper")
unless self.class.const_defined?('Rails')
require 'benchmark'
print "loading environment "
puts("took " + Benchmark.measure do
require 'active_record'
require 'active_support'
require 'active_support/test_case'
require 'spec/test/unit'
require 'spec/rails/extensions/spec/matchers/have'
require 'spec/rails/extensions/active_record/base'
require 'spec/rails/interop/testcase'
require 'spec/runner/configuration'
require 'active_record/test_case'
require 'spec/rails/extensions/spec/matchers/have'
require 'spec/rails/extensions/active_record/base'
require "spec/rails/example/model_example_group"
end.real.to_s)
ENV['RAILS_ENV'] = 'test'
if self.class.const_defined?(:Rails)
puts "#{File.basename(__FILE__)}: Full Rails Environment is already loaded"
require 'spec/rails'
else
print "establishing connection "
require 'erb'
puts("took " + Benchmark.measure do
config_yml = YAML::load(ERB.new(File.read("#{APP_ROOT}/config/database.yml")).result)
ActiveRecord::Base.establish_connection(config_yml['test'])
end.real.to_s)
ActiveRecord::Base.logger = Logger.new("#{APP_ROOT}/log/test.log")
Time.zone = ActiveRecord::Base.default_timezone = 'Eastern Time (US & Canada)'
ActiveRecord::Base.time_zone_aware_attributes = true
end
end
# Author: Lenny
# Date: 20Feb09
#
# This is a spec_helper for inclusion by specs that don't
# need/depend on the entire rails environment such as code
# in the lib or models director of a Rails app. Specs
# that don't load the entire rails environment run significantly
# faster, especially under JRuby.
#
require 'java'
require 'rubygems'
require 'spec'
unless self.class.const_defined?('APP_ROOT')
# For me this file lives in vendor/plugins/myplugin/spec/support
APP_ROOT = File.expand_path(File.dirname(__FILE__) + '/../../../../../')
end
puts "no_rails_env_spec_helper APP_ROOT: #{APP_ROOT}"
if ENV['RAILS_ENV'] && !self.class.const_defined?('Rails')
# The minimal environment spec helpers are not needed if the full Rails
# environment has already been loaded. Any manual 'requiring' of application
# files will cause undesired behavior from class loading contention
# with Rails. Since Rails uses its own Dependencies/require_dependency/require
# mechanism, it will result in application classes being loaded multiple times
# if the Rails env happens to be loaded after the files have been
# manually required(e.g. by a no_rails_env_spec_helper spec). Some classes
# cannot be reloaded without the special unloading that the Rails Dependecies
# machanism does. For example, validation callbacks(e.g. validate_on_create, etc..)
# will be triggered once for each time an ActiveRecord model class is loaded, which
# leads to difficult to track down spec failures(e.g. mocked method expected once bu
# received twice). To avoid the entire situation while still allowing the run
# of individual specs not dependent on Rails to use the fast, minimal
# environment when run individually, we assume that the Rails environment is required
# if RAILS_ENV is set and pre-load it before any manual requires are hit.
puts "#{File.basename(__FILE__)}: RAILS_ENV was set. Loading full Rails environment to avoid class loading contention."
require File.expand_path(APP_ROOT + '/config/environment.rb')
end
if self.class.const_defined?('Rails')
puts "\n\n#{File.basename(__FILE__)}: **Note**\n *Rails environment is already loaded.*\n " +
" This may either be because one of the spec files you " +
"are running has loaded it or because your " +
"script/spec file(used automatically by netbeans) needs editing.\n\n"
else
puts "#{File.basename(__FILE__)}: Full Rails environment not loaded; Setting up minimal environment."
module Kernel
def require_dependency(file)
require(file)
end
end
require 'active_support/core_ext/object/blank'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment