Only call railtie when Rails is defined; assume controller loaded

Isolated Testing

- Rake test inspired by https://github.com/rails/rails/blob/v5.0.0.beta1/activejob/Rakefile
- Isolated unit inspired by
  - https://github.com/rails/rails/blob/v5.0.0.beta1/railties/test/isolation/abstract_unit.rb
  - https://github.com/rails/rails/blob/v5.0.0.beta1/activemodel/test/cases/railtie_test.rb

Misc

- Turns out `mattr_accessor(:logger) {
  ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) }`
  was always nil until the Railtie was loaded, since mattr_accessor
  block defaults don't really work on modules, but on the classes that
  include them.
- Commented on important on Rails being required first for caching to
  work.
- In isolated tests, `active_support/core_ext/object/with_options` is required.
This commit is contained in:
Benjamin Fleischer
2015-12-24 12:21:51 -06:00
parent fe015d17f2
commit 509221c1e0
5 changed files with 194 additions and 15 deletions

View File

@@ -0,0 +1,69 @@
# Execute this test in isolation
require 'support/isolated_unit'
class RailtieTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
class WithRails < RailtieTest
setup do
require 'rails'
require 'active_model_serializers'
make_basic_app
end
test 'mixes ActionController::Serialization into ActionController::Base' do
assert ActionController.const_defined?(:Serialization),
"ActionController::Serialization should be defined, but isn't"
assert ::ActionController::Base.included_modules.include?(::ActionController::Serialization),
"ActionController::Serialization should be included in ActionController::Base, but isn't"
end
test 'sets the ActiveModelSerializers.logger to Rails.logger' do
refute_nil Rails.logger
refute_nil ActiveModelSerializers.logger
assert_equal Rails.logger, ActiveModelSerializers.logger
end
test 'it is configured for caching' do
assert_equal ActionController::Base.cache_store, ActiveModelSerializers.config.cache_store
assert_equal Rails.configuration.action_controller.perform_caching, ActiveModelSerializers.config.perform_caching
end
test 'it runs the load hook' do
loaded = false
ActiveSupport.on_load(:active_model_serializers) { loaded = true }
assert loaded
end
end
class WithoutRails < RailtieTest
setup do
require 'active_model_serializers'
make_basic_app
end
test 'does not mix ActionController::Serialization into ActionController::Base' do
refute ActionController.const_defined?(:Serialization),
'ActionController::Serialization should not be defined, but is'
end
test 'has its own logger at ActiveModelSerializers.logger' do
refute_nil Rails.logger
refute_nil ActiveModelSerializers.logger
refute_equal Rails.logger, ActiveModelSerializers.logger
end
test 'it is not configured for caching' do
refute_nil ActionController::Base.cache_store
assert_nil ActiveModelSerializers.config.cache_store
refute Rails.configuration.action_controller.perform_caching
refute ActiveModelSerializers.config.perform_caching
end
test "it hasn't run the load hook" do
loaded = false
ActiveSupport.on_load(:active_model_serializers) { loaded = true }
refute loaded
end
end
end

View File

@@ -0,0 +1,77 @@
# https://github.com/rails/rails/blob/v5.0.0.beta1/railties/test/isolation/abstract_unit.rb
# Usage Example:
#
# require 'support/isolated_unit'
#
# class RailtieTest < ActiveSupport::TestCase
# include ActiveSupport::Testing::Isolation
#
# class WithRailsDefinedOnLoad < RailtieTest
# setup do
# require 'rails'
# require 'active_model_serializers'
# make_basic_app
# end
#
# # some tests
# end
#
# class WithoutRailsDefinedOnLoad < RailtieTest
# setup do
# require 'active_model_serializers'
# make_basic_app
# end
#
# # some tests
# end
# end
#
# Note:
# It is important to keep this file as light as possible
# the goal for tests that require this is to test booting up
# rails from an empty state, so anything added here could
# hide potential failures
#
# It is also good to know what is the bare minimum to get
# Rails booted up.
require 'bundler/setup' unless defined?(Bundler)
require 'active_support'
require 'active_support/core_ext/string/access'
# These files do not require any others and are needed
# to run the tests
require 'active_support/testing/autorun'
require 'active_support/testing/isolation'
module TestHelpers
module Generation
# Make a very basic app, without creating the whole directory structure.
# Is faster and simpler than generating a Rails app in a temp directory
def make_basic_app
require 'rails'
require 'action_controller/railtie'
@app = Class.new(Rails::Application) do
config.eager_load = false
config.session_store :cookie_store, key: '_myapp_session'
config.active_support.deprecation = :log
config.active_support.test_order = :parallel
ActiveSupport::TestCase.respond_to?(:test_order=) && ActiveSupport::TestCase.test_order = :parallel
config.root = File.dirname(__FILE__)
config.log_level = :info
# Set a fake logger to avoid creating the log directory automatically
fake_logger = Logger.new(nil)
config.logger = fake_logger
end
@app.respond_to?(:secrets) && @app.secrets.secret_key_base = '3b7cd727ee24e8444053437c36cc66c4'
yield @app if block_given?
@app.initialize!
end
end
end
class ActiveSupport::TestCase
include TestHelpers::Generation
end