1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

tests, extract helpers to modify global state.

Make sure that tests do not hardcode the default value.
For example `test_instantiation_doesnt_try_to_require_corresponding_file`
always restored the configuration to `true` regardless of what it's
original value was.

Extract a helper to make the global modification consistent across tests.
This commit is contained in:
Yves Senn 2015-05-07 15:04:49 +02:00
parent 8639795c99
commit d27974d3fb

View file

@ -7,15 +7,32 @@ require 'models/subscriber'
require 'models/vegetables' require 'models/vegetables'
require 'models/shop' require 'models/shop'
module InheritanceTestHelper
def with_store_full_sti_class(&block)
assign_store_full_sti_class true, &block
end
def without_store_full_sti_class(&block)
assign_store_full_sti_class false, &block
end
def assign_store_full_sti_class(flag)
old_store_full_sti_class = ActiveRecord::Base.store_full_sti_class
ActiveRecord::Base.store_full_sti_class = flag
yield
ensure
ActiveRecord::Base.store_full_sti_class = old_store_full_sti_class
end
end
class InheritanceTest < ActiveRecord::TestCase class InheritanceTest < ActiveRecord::TestCase
include InheritanceTestHelper
fixtures :companies, :projects, :subscribers, :accounts, :vegetables fixtures :companies, :projects, :subscribers, :accounts, :vegetables
def test_class_with_store_full_sti_class_returns_full_name def test_class_with_store_full_sti_class_returns_full_name
old = ActiveRecord::Base.store_full_sti_class with_store_full_sti_class do
ActiveRecord::Base.store_full_sti_class = true assert_equal 'Namespaced::Company', Namespaced::Company.sti_name
assert_equal 'Namespaced::Company', Namespaced::Company.sti_name end
ensure
ActiveRecord::Base.store_full_sti_class = old
end end
def test_class_with_blank_sti_name def test_class_with_blank_sti_name
@ -33,39 +50,31 @@ class InheritanceTest < ActiveRecord::TestCase
end end
def test_class_without_store_full_sti_class_returns_demodulized_name def test_class_without_store_full_sti_class_returns_demodulized_name
old = ActiveRecord::Base.store_full_sti_class without_store_full_sti_class do
ActiveRecord::Base.store_full_sti_class = false assert_equal 'Company', Namespaced::Company.sti_name
assert_equal 'Company', Namespaced::Company.sti_name end
ensure
ActiveRecord::Base.store_full_sti_class = old
end end
def test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled def test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled
old = ActiveRecord::Base.store_full_sti_class without_store_full_sti_class do
ActiveRecord::Base.store_full_sti_class = false item = Namespaced::Company.new
item = Namespaced::Company.new assert_equal 'Company', item[:type]
assert_equal 'Company', item[:type] end
ensure
ActiveRecord::Base.store_full_sti_class = old
end end
def test_should_store_full_class_name_with_store_full_sti_class_option_enabled def test_should_store_full_class_name_with_store_full_sti_class_option_enabled
old = ActiveRecord::Base.store_full_sti_class with_store_full_sti_class do
ActiveRecord::Base.store_full_sti_class = true item = Namespaced::Company.new
item = Namespaced::Company.new assert_equal 'Namespaced::Company', item[:type]
assert_equal 'Namespaced::Company', item[:type] end
ensure
ActiveRecord::Base.store_full_sti_class = old
end end
def test_different_namespace_subclass_should_load_correctly_with_store_full_sti_class_option def test_different_namespace_subclass_should_load_correctly_with_store_full_sti_class_option
old = ActiveRecord::Base.store_full_sti_class with_store_full_sti_class do
ActiveRecord::Base.store_full_sti_class = true item = Namespaced::Company.create name: "Wolverine 2"
item = Namespaced::Company.create :name => "Wolverine 2" assert_not_nil Company.find(item.id)
assert_not_nil Company.find(item.id) assert_not_nil Namespaced::Company.find(item.id)
assert_not_nil Namespaced::Company.find(item.id) end
ensure
ActiveRecord::Base.store_full_sti_class = old
end end
def test_company_descends_from_active_record def test_company_descends_from_active_record
@ -331,6 +340,7 @@ class InheritanceTest < ActiveRecord::TestCase
end end
class InheritanceComputeTypeTest < ActiveRecord::TestCase class InheritanceComputeTypeTest < ActiveRecord::TestCase
include InheritanceTestHelper
fixtures :companies fixtures :companies
def setup def setup
@ -344,27 +354,26 @@ class InheritanceComputeTypeTest < ActiveRecord::TestCase
end end
def test_instantiation_doesnt_try_to_require_corresponding_file def test_instantiation_doesnt_try_to_require_corresponding_file
ActiveRecord::Base.store_full_sti_class = false without_store_full_sti_class do
foo = Firm.first.clone foo = Firm.first.clone
foo.type = 'FirmOnTheFly' foo.type = 'FirmOnTheFly'
foo.save! foo.save!
# Should fail without FirmOnTheFly in the type condition. # Should fail without FirmOnTheFly in the type condition.
assert_raise(ActiveRecord::RecordNotFound) { Firm.find(foo.id) } assert_raise(ActiveRecord::RecordNotFound) { Firm.find(foo.id) }
# Nest FirmOnTheFly in the test case where Dependencies won't see it. # Nest FirmOnTheFly in the test case where Dependencies won't see it.
self.class.const_set :FirmOnTheFly, Class.new(Firm) self.class.const_set :FirmOnTheFly, Class.new(Firm)
assert_raise(ActiveRecord::SubclassNotFound) { Firm.find(foo.id) } assert_raise(ActiveRecord::SubclassNotFound) { Firm.find(foo.id) }
# Nest FirmOnTheFly in Firm where Dependencies will see it. # Nest FirmOnTheFly in Firm where Dependencies will see it.
# This is analogous to nesting models in a migration. # This is analogous to nesting models in a migration.
Firm.const_set :FirmOnTheFly, Class.new(Firm) Firm.const_set :FirmOnTheFly, Class.new(Firm)
# And instantiate will find the existing constant rather than trying # And instantiate will find the existing constant rather than trying
# to require firm_on_the_fly. # to require firm_on_the_fly.
assert_nothing_raised { assert_kind_of Firm::FirmOnTheFly, Firm.find(foo.id) } assert_nothing_raised { assert_kind_of Firm::FirmOnTheFly, Firm.find(foo.id) }
ensure end
ActiveRecord::Base.store_full_sti_class = true
end end
def test_sti_type_from_attributes_disabled_in_non_sti_class def test_sti_type_from_attributes_disabled_in_non_sti_class