Translating the length validator error message
In the Agile Web Development book, it is suggested to make a unit test as follows:
assert_equal I18n.translate('activerecord.errors.messages.taken'),
product.errors[:title].join('; ')
I tried using this for other error messages, such as the length validator too_short message, but get errors:
assert_equal I18n.translate('activerecord.errors.messages.too_short', :count=>10),
product.errors[:title].join('; ')
gives:
<"translation missing: en, activerecord, errors, messages, too_short"> expected but was
<"is too short (minimum is 10 characters)">.
A quick google search suggests others are using activerecord.errors.messages.too_short, but apparently it doesn't work. Is there a different way to access the message开发者_JAVA百科, am I doing something else wrong?
ActiveModel handles most of the errors, so the error messages are in errors.messages
. The only exception is taken
, because the uniqueness validation is ActiveRecord specific.
So you'll need:
assert_equal I18n.translate('errors.messages.too_short', :count=>10),
product.errors[:title].join('; ')
Here are the messages: https://github.com/rails/rails/blob/master/activemodel/lib/active_model/locale/en.yml
精彩评论