class I18nValidationTest
Constants
- COMMON_CASES
A
set of common cases forActiveModel::Validations
message generation that are used to generate tests to keep things DRY
Public Class Methods
set_expectations_for_validation(validation, error_type) { |person, {}| ... }
click to toggle source
To make things DRY this macro is created to define 3 tests for every validation case.
# File activemodel/test/cases/validations/i18n_validation_test.rb, line 221 def self.set_expectations_for_validation(validation, error_type, &block_that_sets_validation) if error_type == :confirmation attribute = :title_confirmation else attribute = :title end test "#{validation} finds custom model key translation when #{error_type}" do I18n.backend.store_translations "en", activemodel: { errors: { models: { person: { attributes: { attribute => { error_type => "custom message" } } } } } } I18n.backend.store_translations "en", errors: { messages: { error_type => "global message" } } yield(@person, {}) @person.valid? assert_equal ["custom message"], @person.errors[attribute] end test "#{validation} finds custom model key translation with interpolation when #{error_type}" do I18n.backend.store_translations "en", activemodel: { errors: { models: { person: { attributes: { attribute => { error_type => "custom message with %{extra}" } } } } } } I18n.backend.store_translations "en", errors: { messages: { error_type => "global message" } } yield(@person, { extra: "extra information" }) @person.valid? assert_equal ["custom message with extra information"], @person.errors[attribute] end test "#{validation} finds global default key translation when #{error_type}" do I18n.backend.store_translations "en", errors: { messages: { error_type => "global message" } } yield(@person, {}) @person.valid? assert_equal ["global message"], @person.errors[attribute] end end
Public Instance Methods
replied_topic()
click to toggle source
# File activerecord/test/cases/validations/i18n_validation_test.rb, line 29 def replied_topic @replied_topic ||= begin topic = Topic.create(title: "topic") topic.replies << Reply.new topic end end
setup()
click to toggle source
# File activemodel/test/cases/validations/i18n_validation_test.rb, line 7 def setup Person.clear_validators! @person = Person.new @old_load_path, @old_backend = I18n.load_path.dup, I18n.backend I18n.load_path.clear I18n.backend = I18n::Backend::Simple.new I18n.backend.store_translations("en", errors: { messages: { custom: nil } }) end
teardown()
click to toggle source
# File activemodel/test/cases/validations/i18n_validation_test.rb, line 17 def teardown Person.clear_validators! I18n.load_path.replace @old_load_path I18n.backend = @old_backend I18n.backend.reload! end
test_errors_full_messages_translates_human_attribute_name_for_model_attributes()
click to toggle source
# File activemodel/test/cases/validations/i18n_validation_test.rb, line 32 def test_errors_full_messages_translates_human_attribute_name_for_model_attributes @person.errors.add(:name, "not found") assert_called_with(Person, :human_attribute_name, [:name, default: "Name"], returns: "Person's name") do assert_equal ["Person's name not found"], @person.errors.full_messages end end
test_errors_full_messages_uses_format()
click to toggle source
# File activemodel/test/cases/validations/i18n_validation_test.rb, line 39 def test_errors_full_messages_uses_format I18n.backend.store_translations("en", errors: { format: "Field %{attribute} %{message}" }) @person.errors.add("name", "empty") assert_equal ["Field Name empty"], @person.errors.full_messages end
test_full_message_encoding()
click to toggle source
# File activemodel/test/cases/validations/i18n_validation_test.rb, line 24 def test_full_message_encoding I18n.backend.store_translations("en", errors: { messages: { too_short: "猫舌" } }) Person.validates_length_of :title, within: 3..5 @person.valid? assert_equal ["Title 猫舌"], @person.errors.full_messages end
test_validates_associated_finds_custom_model_key_translation()
click to toggle source
# File activerecord/test/cases/validations/i18n_validation_test.rb, line 69 def test_validates_associated_finds_custom_model_key_translation I18n.backend.store_translations "en", activerecord: { errors: { models: { topic: { attributes: { replies: { invalid: "custom message" } } } } } } I18n.backend.store_translations "en", activerecord: { errors: { messages: { invalid: "global message" } } } Topic.validates_associated :replies replied_topic.valid? assert_equal ["custom message"], replied_topic.errors[:replies].uniq end
test_validates_associated_finds_global_default_translation()
click to toggle source
# File activerecord/test/cases/validations/i18n_validation_test.rb, line 78 def test_validates_associated_finds_global_default_translation I18n.backend.store_translations "en", activerecord: { errors: { messages: { invalid: "global message" } } } Topic.validates_associated :replies replied_topic.valid? assert_equal ["global message"], replied_topic.errors[:replies] end
test_validates_with_message_string()
click to toggle source
# File activemodel/test/cases/validations/i18n_validation_test.rb, line 338 def test_validates_with_message_string Person.validates_presence_of :title, message: "I am a custom error" @person.title = nil @person.valid? assert_equal ["I am a custom error"], @person.errors[:title] end
test_validates_with_message_symbol_must_translate_per_attribute()
click to toggle source
# File activemodel/test/cases/validations/i18n_validation_test.rb, line 322 def test_validates_with_message_symbol_must_translate_per_attribute I18n.backend.store_translations "en", activemodel: { errors: { models: { person: { attributes: { title: { custom_error: "I am a custom error" } } } } } } Person.validates_presence_of :title, message: :custom_error @person.title = nil @person.valid? assert_equal ["I am a custom error"], @person.errors[:title] end
test_validates_with_message_symbol_must_translate_per_model()
click to toggle source
# File activemodel/test/cases/validations/i18n_validation_test.rb, line 330 def test_validates_with_message_symbol_must_translate_per_model I18n.backend.store_translations "en", activemodel: { errors: { models: { person: { custom_error: "I am a custom error" } } } } Person.validates_presence_of :title, message: :custom_error @person.title = nil @person.valid? assert_equal ["I am a custom error"], @person.errors[:title] end
test_validations_with_message_symbol_must_translate()
click to toggle source
# File activemodel/test/cases/validations/i18n_validation_test.rb, line 314 def test_validations_with_message_symbol_must_translate I18n.backend.store_translations "en", errors: { messages: { custom_error: "I am a custom error" } } Person.validates_presence_of :title, message: :custom_error @person.title = nil @person.valid? assert_equal ["I am a custom error"], @person.errors[:title] end
unique_topic()
click to toggle source
# File activerecord/test/cases/validations/i18n_validation_test.rb, line 25 def unique_topic @unique ||= Topic.create title: "unique!" end