module Ndd::RSpec::Rails::Matchers::Model

RSpec matchers for Rails models.

Public Instance Methods

have_a_translated_attribute(attribute) click to toggle source

Ensure that an attribute has an associated translation.

More precisely, ensure that

I18n.t(locale, "activerecord.attributes.{snake_case_class_name}.{snake_case_attribute_name}")

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

@example

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

  it { is_expected.to have_a_translated_attribute(:comment).in_default_locale }
end

@param attribute [String|Symbol] the attribute to test. @return [Ndd::RSpec::Rails::Matchers::Model::HaveATranslatedAttribute]

# File lib/ndd/rspec/rails/matchers/model.rb, line 33
def have_a_translated_attribute(attribute) # rubocop:disable Style/PredicateName
  HaveATranslatedAttribute.new(attribute)
end
have_a_translated_error(error) click to toggle source

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).

@example

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

@param error [String|Symbol] the error to test. @return [Ndd::RSpec::Rails::Matchers::Model::HaveATranslatedError]

# File lib/ndd/rspec/rails/matchers/model.rb, line 67
def have_a_translated_error(error) # rubocop:disable Style/PredicateName
  HaveATranslatedError.new(error)
end
have_a_translated_model() click to toggle source

Ensure that a model has an associated translation.

More precisely, ensure that

I18n.t(locale, "activerecord.models.{snake_case_class_name}")

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

@example

RSpec.describe MyModel, type: :model do
  # both are equivalent
  it { is_expected.to have_a_translated_model }
  it { is_expected.to have_a_translated_model.in_available_locales }

  it { is_expected.to have_a_translated_model.in_default_locale }
end

@return [Ndd::RSpec::Rails::Matchers::Model::HaveATranslatedModel]

# File lib/ndd/rspec/rails/matchers/model.rb, line 90
def have_a_translated_model # rubocop:disable Style/PredicateName
  HaveATranslatedModel.new
end