shoulda-matchers icon indicating copy to clipboard operation
shoulda-matchers copied to clipboard

FIx: Indifference to I18n errors.format translation

Open HeitorMC opened this issue 1 year ago • 3 comments

Fixes #1492

I didn't find anything related to internationalization tests :thinking:, also both spec/unit/shoulda/matchers/active_model/allow_value_matcher_spec.rb and spec/unit/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb passed without errors.

@VSPPedro @mcmire do you have any suggestions on how to test this case, or is this enough?

HeitorMC avatar Mar 17 '23 13:03 HeitorMC

Hello @HeitorMC, thanks for your PR. I'll take a look at how to do a test for this ASAP. Probably tomorrow or next week.

vsppedro avatar Mar 18 '23 20:03 vsppedro

Hey, @VSPPedro any updates on this case?

HeitorMC avatar Mar 29 '23 18:03 HeitorMC

Hi @HeitorMC. Sorry for the delay in responding. We fake i18n in tests. You can see existing usage of this in the following context in the tests for allow_value:

https://github.com/thoughtbot/shoulda-matchers/blob/fababc398deaae12cc94cab6508877fc9f13a946/spec/unit/shoulda/matchers/active_model/allow_value_matcher_spec.rb#L417-L441

It's a bit hard to see because of the indirection, but these tests verify that if with_message is given a symbol, shoulda-matchers uses the same i18n key to resolve that symbol as Rails does. You can see I18n being stubbed here:

https://github.com/thoughtbot/shoulda-matchers/blob/fababc398deaae12cc94cab6508877fc9f13a946/spec/support/unit/record_builder_with_i18n_validation_message.rb#L38

So, I think in order to test your PR, you'd want to add a new context and a new test inside of that test that verifies that when with_message is not specified, but strict is, and errors.format is stubbed to return a certain string that's different than what Rails uses, then the test still passes. You don't have to create a new class like we've done for RecordBuilderWithI18nValidationMessage; I think you could keep it simpler and do something like this:

https://github.com/thoughtbot/shoulda-matchers/blob/fababc398deaae12cc94cab6508877fc9f13a946/spec/unit/shoulda/matchers/active_model/allow_value_matcher_spec.rb#L70-L71

I can suggest something a little more specific later, but let me know if that helps in the meantime.

mcmire avatar Mar 29 '23 19:03 mcmire