mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Ensure that tests will reset belongs_to_requierd_by_default config
This commit is contained in:
parent
835bf75125
commit
6ff8518868
1 changed files with 28 additions and 24 deletions
|
@ -23,18 +23,20 @@ class RequiredAssociationsTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test "belongs_to associations can be optional by default" do
|
test "belongs_to associations can be optional by default" do
|
||||||
original_value = ActiveRecord::Base.belongs_to_required_by_default
|
begin
|
||||||
ActiveRecord::Base.belongs_to_required_by_default = false
|
original_value = ActiveRecord::Base.belongs_to_required_by_default
|
||||||
|
ActiveRecord::Base.belongs_to_required_by_default = false
|
||||||
|
|
||||||
model = subclass_of(Child) do
|
model = subclass_of(Child) do
|
||||||
belongs_to :parent, inverse_of: false,
|
belongs_to :parent, inverse_of: false,
|
||||||
class_name: "RequiredAssociationsTest::Parent"
|
class_name: "RequiredAssociationsTest::Parent"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert model.new.save
|
||||||
|
assert model.new(parent: Parent.new).save
|
||||||
|
ensure
|
||||||
|
ActiveRecord::Base.belongs_to_required_by_default = original_value
|
||||||
end
|
end
|
||||||
|
|
||||||
assert model.new.save
|
|
||||||
assert model.new(parent: Parent.new).save
|
|
||||||
|
|
||||||
ActiveRecord::Base.belongs_to_required_by_default = original_value
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "required belongs_to associations have presence validated" do
|
test "required belongs_to associations have presence validated" do
|
||||||
|
@ -52,22 +54,24 @@ class RequiredAssociationsTest < ActiveRecord::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test "belongs_to associations can be required by default" do
|
test "belongs_to associations can be required by default" do
|
||||||
original_value = ActiveRecord::Base.belongs_to_required_by_default
|
begin
|
||||||
ActiveRecord::Base.belongs_to_required_by_default = true
|
original_value = ActiveRecord::Base.belongs_to_required_by_default
|
||||||
|
ActiveRecord::Base.belongs_to_required_by_default = true
|
||||||
|
|
||||||
model = subclass_of(Child) do
|
model = subclass_of(Child) do
|
||||||
belongs_to :parent, inverse_of: false,
|
belongs_to :parent, inverse_of: false,
|
||||||
class_name: "RequiredAssociationsTest::Parent"
|
class_name: "RequiredAssociationsTest::Parent"
|
||||||
|
end
|
||||||
|
|
||||||
|
record = model.new
|
||||||
|
assert_not record.save
|
||||||
|
assert_equal ["Parent must exist"], record.errors.full_messages
|
||||||
|
|
||||||
|
record.parent = Parent.new
|
||||||
|
assert record.save
|
||||||
|
ensure
|
||||||
|
ActiveRecord::Base.belongs_to_required_by_default = original_value
|
||||||
end
|
end
|
||||||
|
|
||||||
record = model.new
|
|
||||||
assert_not record.save
|
|
||||||
assert_equal ["Parent must exist"], record.errors.full_messages
|
|
||||||
|
|
||||||
record.parent = Parent.new
|
|
||||||
assert record.save
|
|
||||||
|
|
||||||
ActiveRecord::Base.belongs_to_required_by_default = original_value
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "has_one associations are not required by default" do
|
test "has_one associations are not required by default" do
|
||||||
|
|
Loading…
Reference in a new issue