aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/required_test.rb
diff options
context:
space:
mode:
authorHenrik Nygren <nygrenh@gmail.com>2015-01-27 15:52:52 +0200
committerHenrik Nygren <nygrenh@gmail.com>2015-01-28 11:32:10 +0200
commit9a6c6c6f094ce965cc251865bdc1828bc4f38039 (patch)
tree7c25a09f8c5af36370ebb3c8bba22c670207987b /activerecord/test/cases/associations/required_test.rb
parent71a84206ab4d3488ac0d522a7375efc67301aae5 (diff)
downloadrails-9a6c6c6f094ce965cc251865bdc1828bc4f38039.tar.gz
rails-9a6c6c6f094ce965cc251865bdc1828bc4f38039.tar.bz2
rails-9a6c6c6f094ce965cc251865bdc1828bc4f38039.zip
Provide a better error message on :required association
Fixes #18696.
Diffstat (limited to 'activerecord/test/cases/associations/required_test.rb')
-rw-r--r--activerecord/test/cases/associations/required_test.rb24
1 files changed, 22 insertions, 2 deletions
diff --git a/activerecord/test/cases/associations/required_test.rb b/activerecord/test/cases/associations/required_test.rb
index a6123ac432..8b765a2e0c 100644
--- a/activerecord/test/cases/associations/required_test.rb
+++ b/activerecord/test/cases/associations/required_test.rb
@@ -40,7 +40,7 @@ class RequiredAssociationsTest < ActiveRecord::TestCase
record = model.new
assert_not record.save
- assert_equal ["Parent can't be blank"], record.errors.full_messages
+ assert_equal ["Parent must exist"], record.errors.full_messages
record.parent = Parent.new
assert record.save
@@ -64,12 +64,32 @@ class RequiredAssociationsTest < ActiveRecord::TestCase
record = model.new
assert_not record.save
- assert_equal ["Child can't be blank"], record.errors.full_messages
+ assert_equal ["Child must exist"], record.errors.full_messages
record.child = Child.new
assert record.save
end
+ test "required has_one associations have a correct error message" do
+ model = subclass_of(Parent) do
+ has_one :child, required: true, inverse_of: false,
+ class_name: "RequiredAssociationsTest::Child"
+ end
+
+ record = model.create
+ assert_equal ["Child must exist"], record.errors.full_messages
+ end
+
+ test "required belongs_to associations have a correct error message" do
+ model = subclass_of(Child) do
+ belongs_to :parent, required: true, inverse_of: false,
+ class_name: "RequiredAssociationsTest::Parent"
+ end
+
+ record = model.create
+ assert_equal ["Parent must exist"], record.errors.full_messages
+ end
+
private
def subclass_of(klass, &block)