Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indifference to I18n errors.format translation #1492

Open
Mekajiki opened this issue May 10, 2022 · 0 comments
Open

Indifference to I18n errors.format translation #1492

Mekajiki opened this issue May 10, 2022 · 0 comments

Comments

@Mekajiki
Copy link

Mekajiki commented May 10, 2022

Problem

When errors.format translation is defined differently than %{attribute} %{message}, a test using validate_presence_of
matcher with strict seems not to be proved.

Example

If the translations are defined as;

  errors: 
    format: "%{attribute}%{message}"
    messages: 
      blank: を入力してください

And if you write a test as below;

class A
  incude ActiveModel::Model
  include ActiveModel::Validations

  attr_accessor :value
  
  validates :value, presence: true, strict: true
end

class MenuItemTest < ActiveSupport::TestCase
  subject { A.new }
  
  context "Validations" do
    should validate_presence_of(:value).strict
  end
end

The test above isn't proved with message like;

  After setting :value to ‹nil›, the matcher expected the
  A to be invalid and to raise a validation
  exception with message "Value を入力してください". The record was indeed
  invalid, but the exception message was "Valueを入力してください"
  instead.

The matcher seems to expect a space exists between attribute name and message, but actually doesn't.

Possible Solution

Taking errors.format definition into account might be a good solution.
Seeing how Ruby on Rails raise exceptions, it uses errors.format to compose attribute name and message into a string.
https://github.com/rails/rails/blob/4bbd05c90e5a62c0316307b3ca7c2262ed98b567/activemodel/lib/active_model/errors.rb#L290-L294

      I18n.t(:"errors.format", {
        :default   => "%{attribute} %{message}",
        :attribute => attr_name,
        :message   => message
      })

But validation matcher seems to check messages with fixed format.

"#{human_attribute_name} #{default_attribute_message}"

Changing this code into the same manner as in RoR may solve the problem.

@Mekajiki Mekajiki changed the title Indifference to I18n "errors.format" translation Indifference to I18n errors.format translation May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant