Method: Ndd::RSpec::Rails::Matchers::Model#have_a_translated_error

Defined in:
lib/ndd/rspec/rails/matchers/model.rb

#have_a_translated_error(error) ⇒ Ndd::RSpec::Rails::Matchers::Model::HaveATranslatedError

Ensure that an error on a model or an attribute has an associated translation.

More precisely, ensure that (with all parameters being snake case) one of

I18n.t(locale, "activerecord.errors.models.{class_name}.attributes.{attribute_name}.{error_key}")
I18n.t(locale, "activerecord.errors.models.{class_name}.{error_key}")
I18n.t(locale, "activerecord.errors.messages.{error_key}")
I18n.t(locale, "errors.attributes.{attribute_name}.{error_key}")
I18n.t(locale, "errors.messages.{error_key}")

returns a value for the default locale (i.e. I18n.default_locale) or all the available locales (i.e. I18n.available_locales).

Examples:


RSpec.describe MyModel, type: :model do
  # both are equivalent
  it { is_expected.to have_a_translated_error(:duplicate) }
  it { is_expected.to have_a_translated_error(:duplicate).in_available_locales }

  it { is_expected.to have_a_translated_error(:duplicate).in_default_locale }

  # both are equivalent
  it { is_expected.to have_a_translated_error(:duplicate).on_attribute(:comments) }
  it { is_expected.to have_a_translated_error(:duplicate).on_attribute(:comments).in_available_locales }

  it { is_expected.to have_a_translated_error(:duplicate).on_attribute(:comments).in_default_locale }
end

Parameters:

  • error (String|Symbol)

    the error to test.

Returns:


67
68
69
# File 'lib/ndd/rspec/rails/matchers/model.rb', line 67

def have_a_translated_error(error) # rubocop:disable Style/PredicateName
  HaveATranslatedError.new(error)
end