Class: Ndd::RSpec::Rails::Matchers::Model::HaveATranslatedError

Inherits:
TranslationMatcher show all
Defined in:
lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb

Overview

Instance Method Summary collapse

Methods inherited from TranslationMatcher

#in_available_locales, #in_default_locale

Constructor Details

#initialize(error) ⇒ HaveATranslatedError

Returns a new instance of HaveATranslatedError.

Parameters:

  • error (String|Symbol)

    the error to test.


13
14
15
16
# File 'lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb', line 13

def initialize(error)
  super()
  @error = error
end

Instance Method Details

#descriptionString

Returns a description of this matcher.

Returns:

  • (String)

    a description of this matcher.


38
39
40
41
42
43
# File 'lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb', line 38

def description
  description = "have a translated error message for '#{@error}'"
  description << " on '#{@attribute}'" if @attribute.present?
  description << " in #{locales_as_string(@tested_locales)}"
  description
end

#failure_messageString

Returns details about the failure of this matcher.

Returns:

  • (String)

    details about the failure of this matcher.


46
47
48
49
50
51
52
# File 'lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb', line 46

def failure_message
  message = "expected '#{subject_as_string}' to have a translated error message for '#{@error}'\n"
  message << "but none of the following keys was found:\n"
  message << "#{translation_keys.map { |l| "  - #{l}" }.join("\n")}\n"
  message << "for the locales: #{locales_as_string(@failed_locales)}"
  message
end

#matches?(model) ⇒ Boolean

Returns true if the error has an associated translation, false otherwise.

Parameters:

  • model (Object)

    the model being tested.

Returns:

  • (Boolean)

    true if the error has an associated translation, false otherwise.


28
29
30
31
32
33
34
35
# File 'lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb', line 28

def matches?(model)
  @model = model
  @failed_locales = []
  @tested_locales.each do |tested_locale|
    @failed_locales << tested_locale unless translated_in?(tested_locale)
  end
  @failed_locales.empty?
end

#on_attribute(attribute) ⇒ Object

Set the attribute of the error to test.

Parameters:

  • attribute (String|Symbol)

    the attribute associated to the error to test.

Returns:

  • self


21
22
23
24
# File 'lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb', line 21

def on_attribute(attribute)
  @attribute = attribute
  self
end