Class: Shoulda::Matchers::ActiveRecord::HaveValidAssociationMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/beyond_the_api_test_helpers/shoulda/have_valid_association.rb

Instance Method Summary collapse

Constructor Details

#initialize(association) ⇒ HaveValidAssociationMatcher

Returns a new instance of HaveValidAssociationMatcher.



10
11
12
13
# File 'lib/beyond_the_api_test_helpers/shoulda/have_valid_association.rb', line 10

def initialize(association)
  @association = association
  @options = {}
end

Instance Method Details

#descriptionObject



35
36
37
# File 'lib/beyond_the_api_test_helpers/shoulda/have_valid_association.rb', line 35

def description
  "have a valid association called #{@association}"
end

#failure_messageObject Also known as: failure_message_for_should



25
26
27
# File 'lib/beyond_the_api_test_helpers/shoulda/have_valid_association.rb', line 25

def failure_message
  "Expected #{expectation} (#{@missing})"
end

#failure_message_when_negatedObject Also known as: failure_message_for_should_not



30
31
32
# File 'lib/beyond_the_api_test_helpers/shoulda/have_valid_association.rb', line 30

def failure_message_when_negated
  "Did not expect #{expectation}"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
# File 'lib/beyond_the_api_test_helpers/shoulda/have_valid_association.rb', line 15

def matches?(subject)
  @subject = subject

  @expected_message ||= :required

  @subject.send("#{@association}_id=", 999_967)
  @subject.valid?
  @subject.errors[@association].include? I18n.t("errors.messages.#{@expected_message}")
end