Module: Ndd::RSpec::Rails::Matchers::Model

Defined in:
lib/ndd/rspec/rails/matchers/model.rb,
lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb,
lib/ndd/rspec/rails/matchers/model/have_a_translated_model.rb,
lib/ndd/rspec/rails/matchers/model/have_a_translated_attribute.rb

Overview

RSpec matchers for Rails models.

Defined Under Namespace

Classes: HaveATranslatedAttribute, HaveATranslatedError, HaveATranslatedModel

Instance Method Summary collapse

Instance Method Details

#have_a_translated_attribute(attribute) ⇒ Ndd::RSpec::Rails::Matchers::Model::HaveATranslatedAttribute

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

Examples:


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

33
34
35
# 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) ⇒ 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

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

#have_a_translated_modelNdd::RSpec::Rails::Matchers::Model::HaveATranslatedModel

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

Examples:


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

90
91
92
# File 'lib/ndd/rspec/rails/matchers/model.rb', line 90

def have_a_translated_model # rubocop:disable Style/PredicateName
  HaveATranslatedModel.new
end