5 class ActiveRecordValidationsI18nTests
< ActiveSupport
::TestCase
9 @old_load_path, @old_backend = I18n
.load_path
, I18n
.backend
11 I18n
.backend
= I18n
::Backend::Simple.new
12 I18n
.backend
.store_translations('en', :activerecord => {:errors => {:messages => {:custom => nil}}})
17 I18n
.load_path
.replace
@old_load_path
18 I18n
.backend
= @old_backend
22 @unique ||= Topic
.create
:title => 'unique!'
26 @replied_topic ||= begin
27 topic
= Topic
.create(:title => "topic")
28 topic
.replies
<< Reply
.new
33 def reset_callbacks(*models
)
34 models
.each
do |model
|
35 model
.instance_variable_set("@validate_callbacks", ActiveSupport
::Callbacks::CallbackChain.new
)
36 model
.instance_variable_set("@validate_on_create_callbacks", ActiveSupport
::Callbacks::CallbackChain.new
)
37 model
.instance_variable_set("@validate_on_update_callbacks", ActiveSupport
::Callbacks::CallbackChain.new
)
41 def test_default_error_messages_is_deprecated
42 assert_deprecated('ActiveRecord::Errors.default_error_messages') do
43 ActiveRecord
::Errors.default_error_messages
47 def test_percent_s_interpolation_syntax_in_error_messages_still_works
48 ActiveSupport
::Deprecation.silence
do
49 result
= I18n
.t
:does_not_exist, :default => "%s interpolation syntax is deprecated", :value => 'this'
50 assert_equal result
, "this interpolation syntax is deprecated"
54 def test_percent_s_interpolation_syntax_in_error_messages_is_deprecated
55 assert_deprecated('using %s in messages') do
56 I18n
.t
:does_not_exist, :default => "%s interpolation syntax is deprected", :value => 'this'
60 def test_percent_d_interpolation_syntax_in_error_messages_still_works
61 ActiveSupport
::Deprecation.silence
do
62 result
= I18n
.t
:does_not_exist, :default => "%d interpolation syntaxes are deprecated", :count => 2
63 assert_equal result
, "2 interpolation syntaxes are deprecated"
67 def test_percent_d_interpolation_syntax_in_error_messages_is_deprecated
68 assert_deprecated('using %d in messages') do
69 I18n
.t
:does_not_exist, :default => "%d interpolation syntaxes are deprected", :count => 2
73 # ActiveRecord::Errors
74 def test_errors_generate_message_translates_custom_model_attribute_key
76 I18n
.expects(:translate).with(
79 :default => ['Topic'],
80 :scope => [:activerecord, :models]
84 I18n
.expects(:translate).with(
87 :default => ['Title'],
88 :scope => [:activerecord, :attributes]
92 I18n
.expects(:translate).with(
93 :"models.topic.attributes.title.invalid",
95 :scope => [:activerecord, :errors],
97 :"models.topic.invalid",
98 'default from class def error 1',
100 :attribute => "Title",
102 ).returns('default from class def error 1')
104 @topic.errors
.generate_message
:title, :invalid, :default => 'default from class def error 1'
107 def test_errors_generate_message_translates_custom_model_attribute_keys_with_sti
109 I18n
.expects(:translate).with(
112 :default => [:topic, 'Reply'],
113 :scope => [:activerecord, :models]
117 I18n
.expects(:translate).with(
120 :default => [:'topic.title', 'Title'],
121 :scope => [:activerecord, :attributes]
125 I18n
.expects(:translate).with(
126 :"models.reply.attributes.title.invalid",
128 :scope => [:activerecord, :errors],
130 :"models.reply.invalid",
131 :"models.topic.attributes.title.invalid",
132 :"models.topic.invalid",
133 'default from class def',
134 :"messages.invalid"],
136 :attribute => 'Title'
137 ).returns("default from class def")
139 Reply
.new
.errors
.generate_message
:title, :invalid, :default => 'default from class def'
143 def test_errors_add_on_empty_generates_message
144 @topic.errors
.expects(:generate_message).with(:title, :empty, {:default => nil})
145 @topic.errors
.add_on_empty
:title
148 def test_errors_add_on_empty_generates_message_with_custom_default_message
149 @topic.errors
.expects(:generate_message).with(:title, :empty, {:default => 'custom'})
150 @topic.errors
.add_on_empty
:title, 'custom'
153 def test_errors_add_on_blank_generates_message
154 @topic.errors
.expects(:generate_message).with(:title, :blank, {:default => nil})
155 @topic.errors
.add_on_blank
:title
158 def test_errors_add_on_blank_generates_message_with_custom_default_message
159 @topic.errors
.expects(:generate_message).with(:title, :blank, {:default => 'custom'})
160 @topic.errors
.add_on_blank
:title, 'custom'
163 def test_errors_full_messages_translates_human_attribute_name_for_model_attributes
164 @topic.errors
.instance_variable_set
:@errors, { 'title' => ['empty'] }
165 I18n
.expects(:translate).with(:"topic.title", :default => ['Title'], :scope => [:activerecord, :attributes], :count => 1).returns('Title')
166 @topic.errors
.full_messages
:locale => 'en'
169 # ActiveRecord::Validations
170 # validates_confirmation_of w/ mocha
171 def test_validates_confirmation_of_generates_message
172 Topic
.validates_confirmation_of
:title
173 @topic.title_confirmation
= 'foo'
174 @topic.errors
.expects(:generate_message).with(:title, :confirmation, {:default => nil})
178 def test_validates_confirmation_of_generates_message_with_custom_default_message
179 Topic
.validates_confirmation_of
:title, :message => 'custom'
180 @topic.title_confirmation
= 'foo'
181 @topic.errors
.expects(:generate_message).with(:title, :confirmation, {:default => 'custom'})
185 # validates_acceptance_of w/ mocha
187 def test_validates_acceptance_of_generates_message
188 Topic
.validates_acceptance_of
:title, :allow_nil => false
189 @topic.errors
.expects(:generate_message).with(:title, :accepted, {:default => nil})
193 def test_validates_acceptance_of_generates_message_with_custom_default_message
194 Topic
.validates_acceptance_of
:title, :message => 'custom', :allow_nil => false
195 @topic.errors
.expects(:generate_message).with(:title, :accepted, {:default => 'custom'})
199 # validates_presence_of w/ mocha
201 def test_validates_presence_of_generates_message
202 Topic
.validates_presence_of
:title
203 @topic.errors
.expects(:generate_message).with(:title, :blank, {:default => nil})
207 def test_validates_presence_of_generates_message_with_custom_default_message
208 Topic
.validates_presence_of
:title, :message => 'custom'
209 @topic.errors
.expects(:generate_message).with(:title, :blank, {:default => 'custom'})
213 def test_validates_length_of_within_generates_message_with_title_too_short
214 Topic
.validates_length_of
:title, :within => 3..5
215 @topic.errors
.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => nil})
219 def test_validates_length_of_within_generates_message_with_title_too_short_and_custom_default_message
220 Topic
.validates_length_of
:title, :within => 3..5, :too_short => 'custom'
221 @topic.errors
.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => 'custom'})
225 def test_validates_length_of_within_generates_message_with_title_too_long
226 Topic
.validates_length_of
:title, :within => 3..5
227 @topic.title
= 'this title is too long'
228 @topic.errors
.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => nil})
232 def test_validates_length_of_within_generates_message_with_title_too_long_and_custom_default_message
233 Topic
.validates_length_of
:title, :within => 3..5, :too_long => 'custom'
234 @topic.title
= 'this title is too long'
235 @topic.errors
.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => 'custom'})
239 # validates_length_of :within w/ mocha
241 def test_validates_length_of_within_generates_message_with_title_too_short
242 Topic
.validates_length_of
:title, :within => 3..5
243 @topic.errors
.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => nil})
247 def test_validates_length_of_within_generates_message_with_title_too_short_and_custom_default_message
248 Topic
.validates_length_of
:title, :within => 3..5, :too_short => 'custom'
249 @topic.errors
.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => 'custom'})
253 def test_validates_length_of_within_generates_message_with_title_too_long
254 Topic
.validates_length_of
:title, :within => 3..5
255 @topic.title
= 'this title is too long'
256 @topic.errors
.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => nil})
260 def test_validates_length_of_within_generates_message_with_title_too_long_and_custom_default_message
261 Topic
.validates_length_of
:title, :within => 3..5, :too_long => 'custom'
262 @topic.title
= 'this title is too long'
263 @topic.errors
.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => 'custom'})
267 # validates_length_of :is w/ mocha
269 def test_validates_length_of_is_generates_message
270 Topic
.validates_length_of
:title, :is => 5
271 @topic.errors
.expects(:generate_message).with(:title, :wrong_length, {:count => 5, :default => nil})
275 def test_validates_length_of_is_generates_message_with_custom_default_message
276 Topic
.validates_length_of
:title, :is => 5, :message => 'custom'
277 @topic.errors
.expects(:generate_message).with(:title, :wrong_length, {:count => 5, :default => 'custom'})
281 # validates_uniqueness_of w/ mocha
283 def test_validates_uniqueness_of_generates_message
284 Topic
.validates_uniqueness_of
:title
285 @topic.title
= unique_topic
.title
286 @topic.errors
.expects(:generate_message).with(:title, :taken, {:default => nil, :value => 'unique!'})
290 def test_validates_uniqueness_of_generates_message_with_custom_default_message
291 Topic
.validates_uniqueness_of
:title, :message => 'custom'
292 @topic.title
= unique_topic
.title
293 @topic.errors
.expects(:generate_message).with(:title, :taken, {:default => 'custom', :value => 'unique!'})
297 # validates_format_of w/ mocha
299 def test_validates_format_of_generates_message
300 Topic
.validates_format_of
:title, :with => /^[1-9][0-9]*$/
302 @topic.errors
.expects(:generate_message).with(:title, :invalid, {:value => '72x', :default => nil})
306 def test_validates_format_of_generates_message_with_custom_default_message
307 Topic
.validates_format_of
:title, :with => /^[1-9][0-9]*$/, :message => 'custom'
309 @topic.errors
.expects(:generate_message).with(:title, :invalid, {:value => '72x', :default => 'custom'})
313 # validates_inclusion_of w/ mocha
315 def test_validates_inclusion_of_generates_message
316 Topic
.validates_inclusion_of
:title, :in => %w(a b c
)
318 @topic.errors
.expects(:generate_message).with(:title, :inclusion, {:value => 'z', :default => nil})
322 def test_validates_inclusion_of_generates_message_with_custom_default_message
323 Topic
.validates_inclusion_of
:title, :in => %w(a b c
), :message => 'custom'
325 @topic.errors
.expects(:generate_message).with(:title, :inclusion, {:value => 'z', :default => 'custom'})
329 # validates_exclusion_of w/ mocha
331 def test_validates_exclusion_of_generates_message
332 Topic
.validates_exclusion_of
:title, :in => %w(a b c
)
334 @topic.errors
.expects(:generate_message).with(:title, :exclusion, {:value => 'a', :default => nil})
338 def test_validates_exclusion_of_generates_message_with_custom_default_message
339 Topic
.validates_exclusion_of
:title, :in => %w(a b c
), :message => 'custom'
341 @topic.errors
.expects(:generate_message).with(:title, :exclusion, {:value => 'a', :default => 'custom'})
345 # validates_numericality_of without :only_integer w/ mocha
347 def test_validates_numericality_of_generates_message
348 Topic
.validates_numericality_of
:title
350 @topic.errors
.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => nil})
354 def test_validates_numericality_of_generates_message_with_custom_default_message
355 Topic
.validates_numericality_of
:title, :message => 'custom'
357 @topic.errors
.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => 'custom'})
361 # validates_numericality_of with :only_integer w/ mocha
363 def test_validates_numericality_of_only_integer_generates_message
364 Topic
.validates_numericality_of
:title, :only_integer => true
366 @topic.errors
.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => nil})
370 def test_validates_numericality_of_only_integer_generates_message_with_custom_default_message
371 Topic
.validates_numericality_of
:title, :only_integer => true, :message => 'custom'
373 @topic.errors
.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => 'custom'})
377 # validates_numericality_of :odd w/ mocha
379 def test_validates_numericality_of_odd_generates_message
380 Topic
.validates_numericality_of
:title, :only_integer => true, :odd => true
382 @topic.errors
.expects(:generate_message).with(:title, :odd, {:value => 0, :default => nil})
386 def test_validates_numericality_of_odd_generates_message_with_custom_default_message
387 Topic
.validates_numericality_of
:title, :only_integer => true, :odd => true, :message => 'custom'
389 @topic.errors
.expects(:generate_message).with(:title, :odd, {:value => 0, :default => 'custom'})
393 # validates_numericality_of :less_than w/ mocha
395 def test_validates_numericality_of_less_than_generates_message
396 Topic
.validates_numericality_of
:title, :only_integer => true, :less_than => 0
398 @topic.errors
.expects(:generate_message).with(:title, :less_than, {:value => 1, :count => 0, :default => nil})
402 def test_validates_numericality_of_odd_generates_message_with_custom_default_message
403 Topic
.validates_numericality_of
:title, :only_integer => true, :less_than => 0, :message => 'custom'
405 @topic.errors
.expects(:generate_message).with(:title, :less_than, {:value => 1, :count => 0, :default => 'custom'})
409 # validates_associated w/ mocha
411 def test_validates_associated_generates_message
412 Topic
.validates_associated
:replies
413 replied_topic
.errors
.expects(:generate_message).with(:replies, :invalid, {:value => replied_topic
.replies
, :default => nil})
417 def test_validates_associated_generates_message_with_custom_default_message
418 Topic
.validates_associated
:replies
419 replied_topic
.errors
.expects(:generate_message).with(:replies, :invalid, {:value => replied_topic
.replies
, :default => nil})
423 # validates_confirmation_of w/o mocha
425 def test_validates_confirmation_of_finds_custom_model_key_translation
426 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:confirmation => 'custom message'}}}}}}
427 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:confirmation => 'global message'}}}
429 Topic
.validates_confirmation_of
:title
430 @topic.title_confirmation
= 'foo'
432 assert_equal
'custom message', @topic.errors
.on(:title)
435 def test_validates_confirmation_of_finds_global_default_translation
436 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:confirmation => 'global message'}}}
438 Topic
.validates_confirmation_of
:title
439 @topic.title_confirmation
= 'foo'
441 assert_equal
'global message', @topic.errors
.on(:title)
444 # validates_acceptance_of w/o mocha
446 def test_validates_acceptance_of_finds_custom_model_key_translation
447 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:accepted => 'custom message'}}}}}}
448 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}}
450 Topic
.validates_acceptance_of
:title, :allow_nil => false
452 assert_equal
'custom message', @topic.errors
.on(:title)
455 def test_validates_acceptance_of_finds_global_default_translation
456 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}}
458 Topic
.validates_acceptance_of
:title, :allow_nil => false
460 assert_equal
'global message', @topic.errors
.on(:title)
463 # validates_presence_of w/o mocha
465 def test_validates_presence_of_finds_custom_model_key_translation
466 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:blank => 'custom message'}}}}}}
467 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:blank => 'global message'}}}
469 Topic
.validates_presence_of
:title
471 assert_equal
'custom message', @topic.errors
.on(:title)
474 def test_validates_presence_of_finds_global_default_translation
475 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:blank => 'global message'}}}
477 Topic
.validates_presence_of
:title
479 assert_equal
'global message', @topic.errors
.on(:title)
482 # validates_length_of :within w/o mocha
484 def test_validates_length_of_within_finds_custom_model_key_translation
485 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:too_short => 'custom message'}}}}}}
486 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}}
488 Topic
.validates_length_of
:title, :within => 3..5
490 assert_equal
'custom message', @topic.errors
.on(:title)
493 def test_validates_length_of_within_finds_global_default_translation
494 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}}
496 Topic
.validates_length_of
:title, :within => 3..5
498 assert_equal
'global message', @topic.errors
.on(:title)
501 # validates_length_of :is w/o mocha
503 def test_validates_length_of_is_finds_custom_model_key_translation
504 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}}
505 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}}
507 Topic
.validates_length_of
:title, :is => 5
509 assert_equal
'custom message', @topic.errors
.on(:title)
512 def test_validates_length_of_is_finds_global_default_translation
513 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}}
515 Topic
.validates_length_of
:title, :is => 5
517 assert_equal
'global message', @topic.errors
.on(:title)
520 # validates_uniqueness_of w/o mocha
522 def test_validates_length_of_is_finds_custom_model_key_translation
523 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}}
524 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}}
526 Topic
.validates_length_of
:title, :is => 5
528 assert_equal
'custom message', @topic.errors
.on(:title)
531 def test_validates_length_of_is_finds_global_default_translation
532 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}}
534 Topic
.validates_length_of
:title, :is => 5
536 assert_equal
'global message', @topic.errors
.on(:title)
540 # validates_format_of w/o mocha
542 def test_validates_format_of_finds_custom_model_key_translation
543 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:invalid => 'custom message'}}}}}}
544 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}}
546 Topic
.validates_format_of
:title, :with => /^[1-9][0-9]*$/
548 assert_equal
'custom message', @topic.errors
.on(:title)
551 def test_validates_format_of_finds_global_default_translation
552 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}}
554 Topic
.validates_format_of
:title, :with => /^[1-9][0-9]*$/
556 assert_equal
'global message', @topic.errors
.on(:title)
559 # validates_inclusion_of w/o mocha
561 def test_validates_inclusion_of_finds_custom_model_key_translation
562 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:inclusion => 'custom message'}}}}}}
563 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}}
565 Topic
.validates_inclusion_of
:title, :in => %w(a b c
)
567 assert_equal
'custom message', @topic.errors
.on(:title)
570 def test_validates_inclusion_of_finds_global_default_translation
571 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}}
573 Topic
.validates_inclusion_of
:title, :in => %w(a b c
)
575 assert_equal
'global message', @topic.errors
.on(:title)
578 # validates_exclusion_of w/o mocha
580 def test_validates_exclusion_of_finds_custom_model_key_translation
581 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:exclusion => 'custom message'}}}}}}
582 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:exclusion => 'global message'}}}
584 Topic
.validates_exclusion_of
:title, :in => %w(a b c
)
587 assert_equal
'custom message', @topic.errors
.on(:title)
590 def test_validates_exclusion_of_finds_global_default_translation
591 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:exclusion => 'global message'}}}
593 Topic
.validates_exclusion_of
:title, :in => %w(a b c
)
596 assert_equal
'global message', @topic.errors
.on(:title)
599 # validates_numericality_of without :only_integer w/o mocha
601 def test_validates_numericality_of_finds_custom_model_key_translation
602 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}}
603 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}}
605 Topic
.validates_numericality_of
:title
608 assert_equal
'custom message', @topic.errors
.on(:title)
611 def test_validates_numericality_of_finds_global_default_translation
612 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}}
614 Topic
.validates_numericality_of
:title, :only_integer => true
617 assert_equal
'global message', @topic.errors
.on(:title)
620 # validates_numericality_of with :only_integer w/o mocha
622 def test_validates_numericality_of_only_integer_finds_custom_model_key_translation
623 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}}
624 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}}
626 Topic
.validates_numericality_of
:title, :only_integer => true
629 assert_equal
'custom message', @topic.errors
.on(:title)
632 def test_validates_numericality_of_only_integer_finds_global_default_translation
633 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}}
635 Topic
.validates_numericality_of
:title, :only_integer => true
638 assert_equal
'global message', @topic.errors
.on(:title)
641 # validates_numericality_of :odd w/o mocha
643 def test_validates_numericality_of_odd_finds_custom_model_key_translation
644 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:odd => 'custom message'}}}}}}
645 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:odd => 'global message'}}}
647 Topic
.validates_numericality_of
:title, :only_integer => true, :odd => true
650 assert_equal
'custom message', @topic.errors
.on(:title)
653 def test_validates_numericality_of_odd_finds_global_default_translation
654 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:odd => 'global message'}}}
656 Topic
.validates_numericality_of
:title, :only_integer => true, :odd => true
659 assert_equal
'global message', @topic.errors
.on(:title)
662 # validates_numericality_of :less_than w/o mocha
664 def test_validates_numericality_of_less_than_finds_custom_model_key_translation
665 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:less_than => 'custom message'}}}}}}
666 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:less_than => 'global message'}}}
668 Topic
.validates_numericality_of
:title, :only_integer => true, :less_than => 0
671 assert_equal
'custom message', @topic.errors
.on(:title)
674 def test_validates_numericality_of_less_than_finds_global_default_translation
675 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:less_than => 'global message'}}}
677 Topic
.validates_numericality_of
:title, :only_integer => true, :less_than => 0
680 assert_equal
'global message', @topic.errors
.on(:title)
684 # validates_associated w/o mocha
686 def test_validates_associated_finds_custom_model_key_translation
687 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:replies => {:invalid => 'custom message'}}}}}}
688 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}}
690 Topic
.validates_associated
:replies
692 assert_equal
'custom message', replied_topic
.errors
.on(:replies)
695 def test_validates_associated_finds_global_default_translation
696 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}}
698 Topic
.validates_associated
:replies
700 assert_equal
'global message', replied_topic
.errors
.on(:replies)
703 def test_validations_with_message_symbol_must_translate
704 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:messages => {:custom_error => "I am a custom error"}}}
705 Topic
.validates_presence_of
:title, :message => :custom_error
708 assert_equal
"I am a custom error", @topic.errors
.on(:title)
711 def test_validates_with_message_symbol_must_translate_per_attribute
712 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}}}
713 Topic
.validates_presence_of
:title, :message => :custom_error
716 assert_equal
"I am a custom error", @topic.errors
.on(:title)
719 def test_validates_with_message_symbol_must_translate_per_model
720 I18n
.backend
.store_translations
'en', :activerecord => {:errors => {:models => {:topic => {:custom_error => "I am a custom error"}}}}
721 Topic
.validates_presence_of
:title, :message => :custom_error
724 assert_equal
"I am a custom error", @topic.errors
.on(:title)
727 def test_validates_with_message_string
728 Topic
.validates_presence_of
:title, :message => "I am a custom error"
731 assert_equal
"I am a custom error", @topic.errors
.on(:title)
736 class ActiveRecordValidationsGenerateMessageI18nTests
< Test
::Unit::TestCase
738 reset_callbacks Topic
740 I18n
.backend
.store_translations
:'en', {
744 :inclusion => "is not included in the list",
745 :exclusion => "is reserved",
746 :invalid => "is invalid",
747 :confirmation => "doesn't match confirmation",
748 :accepted => "must be accepted",
749 :empty => "can't be empty",
750 :blank => "can't be blank",
751 :too_long => "is too long (maximum is {{count}} characters)",
752 :too_short => "is too short (minimum is {{count}} characters)",
753 :wrong_length => "is the wrong length (should be {{count}} characters)",
754 :taken => "has already been taken",
755 :not_a_number => "is not a number",
756 :greater_than => "must be greater than {{count}}",
757 :greater_than_or_equal_to => "must be greater than or equal to {{count}}",
758 :equal_to => "must be equal to {{count}}",
759 :less_than => "must be less than {{count}}",
760 :less_than_or_equal_to => "must be less than or equal to {{count}}",
761 :odd => "must be odd",
762 :even => "must be even"
769 def reset_callbacks(*models
)
770 models
.each
do |model
|
771 model
.instance_variable_set("@validate_callbacks", ActiveSupport
::Callbacks::CallbackChain.new
)
772 model
.instance_variable_set("@validate_on_create_callbacks", ActiveSupport
::Callbacks::CallbackChain.new
)
773 model
.instance_variable_set("@validate_on_update_callbacks", ActiveSupport
::Callbacks::CallbackChain.new
)
777 # validates_inclusion_of: generate_message(attr_name, :inclusion, :default => configuration[:message], :value => value)
778 def test_generate_message_inclusion_with_default_message
779 assert_equal
'is not included in the list', @topic.errors
.generate_message(:title, :inclusion, :default => nil, :value => 'title')
782 def test_generate_message_inclusion_with_custom_message
783 assert_equal
'custom message title', @topic.errors
.generate_message(:title, :inclusion, :default => 'custom message {{value}}', :value => 'title')
786 # validates_exclusion_of: generate_message(attr_name, :exclusion, :default => configuration[:message], :value => value)
787 def test_generate_message_exclusion_with_default_message
788 assert_equal
'is reserved', @topic.errors
.generate_message(:title, :exclusion, :default => nil, :value => 'title')
791 def test_generate_message_exclusion_with_custom_message
792 assert_equal
'custom message title', @topic.errors
.generate_message(:title, :exclusion, :default => 'custom message {{value}}', :value => 'title')
795 # validates_associated: generate_message(attr_name, :invalid, :default => configuration[:message], :value => value)
796 # validates_format_of: generate_message(attr_name, :invalid, :default => configuration[:message], :value => value)
797 def test_generate_message_invalid_with_default_message
798 assert_equal
'is invalid', @topic.errors
.generate_message(:title, :invalid, :default => nil, :value => 'title')
801 def test_generate_message_invalid_with_custom_message
802 assert_equal
'custom message title', @topic.errors
.generate_message(:title, :invalid, :default => 'custom message {{value}}', :value => 'title')
805 # validates_confirmation_of: generate_message(attr_name, :confirmation, :default => configuration[:message])
806 def test_generate_message_confirmation_with_default_message
807 assert_equal
"doesn't match confirmation", @topic.errors
.generate_message(:title, :confirmation, :default => nil)
810 def test_generate_message_confirmation_with_custom_message
811 assert_equal
'custom message', @topic.errors
.generate_message(:title, :confirmation, :default => 'custom message')
814 # validates_acceptance_of: generate_message(attr_name, :accepted, :default => configuration[:message])
815 def test_generate_message_accepted_with_default_message
816 assert_equal
"must be accepted", @topic.errors
.generate_message(:title, :accepted, :default => nil)
819 def test_generate_message_accepted_with_custom_message
820 assert_equal
'custom message', @topic.errors
.generate_message(:title, :accepted, :default => 'custom message')
823 # add_on_empty: generate_message(attr, :empty, :default => custom_message)
824 def test_generate_message_empty_with_default_message
825 assert_equal
"can't be empty", @topic.errors
.generate_message(:title, :empty, :default => nil)
828 def test_generate_message_empty_with_custom_message
829 assert_equal
'custom message', @topic.errors
.generate_message(:title, :empty, :default => 'custom message')
832 # add_on_blank: generate_message(attr, :blank, :default => custom_message)
833 def test_generate_message_blank_with_default_message
834 assert_equal
"can't be blank", @topic.errors
.generate_message(:title, :blank, :default => nil)
837 def test_generate_message_blank_with_custom_message
838 assert_equal
'custom message', @topic.errors
.generate_message(:title, :blank, :default => 'custom message')
841 # validates_length_of: generate_message(attr, :too_long, :default => options[:too_long], :count => option_value.end)
842 def test_generate_message_too_long_with_default_message
843 assert_equal
"is too long (maximum is 10 characters)", @topic.errors
.generate_message(:title, :too_long, :default => nil, :count => 10)
846 def test_generate_message_too_long_with_custom_message
847 assert_equal
'custom message 10', @topic.errors
.generate_message(:title, :too_long, :default => 'custom message {{count}}', :count => 10)
850 # validates_length_of: generate_message(attr, :too_short, :default => options[:too_short], :count => option_value.begin)
851 def test_generate_message_too_short_with_default_message
852 assert_equal
"is too short (minimum is 10 characters)", @topic.errors
.generate_message(:title, :too_short, :default => nil, :count => 10)
855 def test_generate_message_too_short_with_custom_message
856 assert_equal
'custom message 10', @topic.errors
.generate_message(:title, :too_short, :default => 'custom message {{count}}', :count => 10)
859 # validates_length_of: generate_message(attr, key, :default => custom_message, :count => option_value)
860 def test_generate_message_wrong_length_with_default_message
861 assert_equal
"is the wrong length (should be 10 characters)", @topic.errors
.generate_message(:title, :wrong_length, :default => nil, :count => 10)
864 def test_generate_message_wrong_length_with_custom_message
865 assert_equal
'custom message 10', @topic.errors
.generate_message(:title, :wrong_length, :default => 'custom message {{count}}', :count => 10)
868 # validates_uniqueness_of: generate_message(attr_name, :taken, :default => configuration[:message])
869 def test_generate_message_taken_with_default_message
870 assert_equal
"has already been taken", @topic.errors
.generate_message(:title, :taken, :default => nil, :value => 'title')
873 def test_generate_message_taken_with_custom_message
874 assert_equal
'custom message title', @topic.errors
.generate_message(:title, :taken, :default => 'custom message {{value}}', :value => 'title')
877 # validates_numericality_of: generate_message(attr_name, :not_a_number, :value => raw_value, :default => configuration[:message])
878 def test_generate_message_not_a_number_with_default_message
879 assert_equal
"is not a number", @topic.errors
.generate_message(:title, :not_a_number, :default => nil, :value => 'title')
882 def test_generate_message_not_a_number_with_custom_message
883 assert_equal
'custom message title', @topic.errors
.generate_message(:title, :not_a_number, :default => 'custom message {{value}}', :value => 'title')
886 # validates_numericality_of: generate_message(attr_name, option, :value => raw_value, :default => configuration[:message])
887 def test_generate_message_greater_than_with_default_message
888 assert_equal
"must be greater than 10", @topic.errors
.generate_message(:title, :greater_than, :default => nil, :value => 'title', :count => 10)
891 def test_generate_message_greater_than_or_equal_to_with_default_message
892 assert_equal
"must be greater than or equal to 10", @topic.errors
.generate_message(:title, :greater_than_or_equal_to, :default => nil, :value => 'title', :count => 10)
895 def test_generate_message_equal_to_with_default_message
896 assert_equal
"must be equal to 10", @topic.errors
.generate_message(:title, :equal_to, :default => nil, :value => 'title', :count => 10)
899 def test_generate_message_less_than_with_default_message
900 assert_equal
"must be less than 10", @topic.errors
.generate_message(:title, :less_than, :default => nil, :value => 'title', :count => 10)
903 def test_generate_message_less_than_or_equal_to_with_default_message
904 assert_equal
"must be less than or equal to 10", @topic.errors
.generate_message(:title, :less_than_or_equal_to, :default => nil, :value => 'title', :count => 10)
907 def test_generate_message_odd_with_default_message
908 assert_equal
"must be odd", @topic.errors
.generate_message(:title, :odd, :default => nil, :value => 'title', :count => 10)
911 def test_generate_message_even_with_default_message
912 assert_equal
"must be even", @topic.errors
.generate_message(:title, :even, :default => nil, :value => 'title', :count => 10)