Class: SequelSpec::Matchers::Association::AssociationMatcher

Inherits:
Base
  • Object
show all
Defined in:
lib/sequel_spec/association/association_matcher.rb

Instance Method Summary collapse

Methods inherited from Base

#failure_message, #hash_to_nice_string, #initialize, #matches?, #negative_failure_message, #with_options

Constructor Details

This class inherits a constructor from SequelSpec::Matchers::Base

Instance Method Details

#descriptionObject



5
6
7
8
9
# File 'lib/sequel_spec/association/association_matcher.rb', line 5

def description
  desc = "have a #{association_type} association #{@attribute.inspect}"
  desc << " with option(s) #{hash_to_nice_string @options}" unless @options.empty?
  desc
end

#valid?(db, instance, klass, attribute, options) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sequel_spec/association/association_matcher.rb', line 11

def valid?(db, instance, klass, attribute, options)
  @association = klass.association_reflection(attribute) || {}

  if @association.empty?
    @suffix << "(no association #{@attribute.inspect} found)"
    false
  else
    matching = @association[:type] == association_type
    options.each do |key, value|
      if @association[key] != value
        @suffix << "expected #{key.inspect} == #{value.inspect} but found #{@association[key].inspect} instead"
        matching = false
      end
    end

    matching
  end
end