aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorChris Salzberg <chris@dejimata.com>2019-04-14 16:53:51 +0900
committerChris Salzberg <chris@dejimata.com>2019-04-14 17:21:49 +0900
commit2e27801145954bb7754a59a8dc55134973b1518f (patch)
tree29ee1300b9949fb175ae5f0c5cfc90eee46f06a0 /activemodel
parent98d4a68a80468429f1da5414772ee6644975a04f (diff)
downloadrails-2e27801145954bb7754a59a8dc55134973b1518f.tar.gz
rails-2e27801145954bb7754a59a8dc55134973b1518f.tar.bz2
rails-2e27801145954bb7754a59a8dc55134973b1518f.zip
Ensure multiple anonymous modules are not included into Topic in tests
Each acceptance validator applied to a model class includes an instance of a module builder (LazilyDefineAttributes) into that class. In tests, if the original model class is not subclassed, these modules pile up and cannot be removed, potentially leading to flakey specs and false positive/negatives. To avoid this, always use subclasses in tests whose names (constants) can be removed when the test is done.
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/test/cases/validations/acceptance_validation_test.rb50
1 files changed, 32 insertions, 18 deletions
diff --git a/activemodel/test/cases/validations/acceptance_validation_test.rb b/activemodel/test/cases/validations/acceptance_validation_test.rb
index 7662f996ae..72baf6e7a7 100644
--- a/activemodel/test/cases/validations/acceptance_validation_test.rb
+++ b/activemodel/test/cases/validations/acceptance_validation_test.rb
@@ -7,21 +7,23 @@ require "models/reply"
require "models/person"
class AcceptanceValidationTest < ActiveModel::TestCase
- def teardown
- Topic.clear_validators!
+ teardown do
+ self.class.send(:remove_const, :TestClass)
end
def test_terms_of_service_agreement_no_acceptance
- Topic.validates_acceptance_of(:terms_of_service)
+ klass = define_test_class(Topic)
+ klass.validates_acceptance_of(:terms_of_service)
- t = Topic.new("title" => "We should not be confirmed")
+ t = klass.new("title" => "We should not be confirmed")
assert_predicate t, :valid?
end
def test_terms_of_service_agreement
- Topic.validates_acceptance_of(:terms_of_service)
+ klass = define_test_class(Topic)
+ klass.validates_acceptance_of(:terms_of_service)
- t = Topic.new("title" => "We should be confirmed", "terms_of_service" => "")
+ t = klass.new("title" => "We should be confirmed", "terms_of_service" => "")
assert_predicate t, :invalid?
assert_equal ["must be accepted"], t.errors[:terms_of_service]
@@ -30,9 +32,10 @@ class AcceptanceValidationTest < ActiveModel::TestCase
end
def test_eula
- Topic.validates_acceptance_of(:eula, message: "must be abided")
+ klass = define_test_class(Topic)
+ klass.validates_acceptance_of(:eula, message: "must be abided")
- t = Topic.new("title" => "We should be confirmed", "eula" => "")
+ t = klass.new("title" => "We should be confirmed", "eula" => "")
assert_predicate t, :invalid?
assert_equal ["must be abided"], t.errors[:eula]
@@ -41,9 +44,10 @@ class AcceptanceValidationTest < ActiveModel::TestCase
end
def test_terms_of_service_agreement_with_accept_value
- Topic.validates_acceptance_of(:terms_of_service, accept: "I agree.")
+ klass = define_test_class(Topic)
+ klass.validates_acceptance_of(:terms_of_service, accept: "I agree.")
- t = Topic.new("title" => "We should be confirmed", "terms_of_service" => "")
+ t = klass.new("title" => "We should be confirmed", "terms_of_service" => "")
assert_predicate t, :invalid?
assert_equal ["must be accepted"], t.errors[:terms_of_service]
@@ -52,9 +56,10 @@ class AcceptanceValidationTest < ActiveModel::TestCase
end
def test_terms_of_service_agreement_with_multiple_accept_values
- Topic.validates_acceptance_of(:terms_of_service, accept: [1, "I concur."])
+ klass = define_test_class(Topic)
+ klass.validates_acceptance_of(:terms_of_service, accept: [1, "I concur."])
- t = Topic.new("title" => "We should be confirmed", "terms_of_service" => "")
+ t = klass.new("title" => "We should be confirmed", "terms_of_service" => "")
assert_predicate t, :invalid?
assert_equal ["must be accepted"], t.errors[:terms_of_service]
@@ -66,9 +71,10 @@ class AcceptanceValidationTest < ActiveModel::TestCase
end
def test_validates_acceptance_of_for_ruby_class
- Person.validates_acceptance_of :karma
+ klass = define_test_class(Person)
+ klass.validates_acceptance_of :karma
- p = Person.new
+ p = klass.new
p.karma = ""
assert_predicate p, :invalid?
@@ -76,13 +82,21 @@ class AcceptanceValidationTest < ActiveModel::TestCase
p.karma = "1"
assert_predicate p, :valid?
- ensure
- Person.clear_validators!
end
def test_validates_acceptance_of_true
- Topic.validates_acceptance_of(:terms_of_service)
+ klass = define_test_class(Topic)
+ klass.validates_acceptance_of(:terms_of_service)
- assert_predicate Topic.new(terms_of_service: true), :valid?
+ assert_predicate klass.new(terms_of_service: true), :valid?
end
+
+ private
+
+ # Acceptance validator includes anonymous module into class, which cannot
+ # be cleared, so to avoid multiple inclusions we use a named subclass which
+ # we can remove in teardown.
+ def define_test_class(parent)
+ self.class.const_set(:TestClass, Class.new(parent))
+ end
end