@@ -143,6 +143,7 @@ def test_reflection_klass_not_found_with_no_class_name_option
143
143
UserWithInvalidRelation . reflect_on_association ( :not_a_class ) . klass
144
144
end
145
145
146
+ assert_equal "NotAClass" , error . name
146
147
assert_match %r/missing/i , error . message
147
148
assert_match "NotAClass" , error . message
148
149
assert_match "UserWithInvalidRelation#not_a_class" , error . message
@@ -154,6 +155,7 @@ def test_reflection_klass_not_found_with_pointer_to_non_existent_class_name
154
155
UserWithInvalidRelation . reflect_on_association ( :class_name_provided_not_a_class ) . klass
155
156
end
156
157
158
+ assert_equal "NotAClass" , error . name
157
159
assert_match %r/missing/i , error . message
158
160
assert_match %r/\b NotAClass\b / , error . message
159
161
assert_match "UserWithInvalidRelation#class_name_provided_not_a_class" , error . message
@@ -560,11 +562,41 @@ def test_reflect_on_association_accepts_strings
560
562
end
561
563
end
562
564
565
+ def test_name_error_from_incidental_code_is_not_converted_to_name_error_for_association
566
+ UserWithInvalidRelation . stub ( :const_missing , proc { oops } ) do
567
+ reflection = UserWithInvalidRelation . reflect_on_association ( :not_a_class )
568
+
569
+ error = assert_raises ( NameError ) do
570
+ reflection . klass
571
+ end
572
+
573
+ assert_equal :oops , error . name
574
+ assert_match "oops" , error . message
575
+ assert_no_match "NotAClass" , error . message
576
+ assert_no_match "not_a_class" , error . message
577
+ end
578
+ end
579
+
563
580
def test_automatic_inverse_suppresses_name_error_for_association
564
581
reflection = UserWithInvalidRelation . reflect_on_association ( :not_a_class )
565
582
assert_not reflection . dup . has_inverse? # dup to prevent global memoization
566
583
end
567
584
585
+ def test_automatic_inverse_does_not_suppress_name_error_from_incidental_code
586
+ UserWithInvalidRelation . stub ( :const_missing , proc { oops } ) do
587
+ reflection = UserWithInvalidRelation . reflect_on_association ( :not_a_class )
588
+
589
+ error = assert_raises ( NameError ) do
590
+ reflection . dup . has_inverse? # dup to prevent global memoization
591
+ end
592
+
593
+ assert_equal :oops , error . name
594
+ assert_match "oops" , error . message
595
+ assert_no_match "NotAClass" , error . message
596
+ assert_no_match "not_a_class" , error . message
597
+ end
598
+ end
599
+
568
600
private
569
601
def assert_reflection ( klass , association , options )
570
602
assert reflection = klass . reflect_on_association ( association )
0 commit comments