Class: RSpec::Hal::Matchers::RelationMatcher

Inherits:
Object
  • Object
show all
Includes:
HalMatcherHelpers
Defined in:
lib/rspec/hal/matchers/relation_matcher.rb

Overview

Example

expect(doc).to have_relation("search")
expect(doc).to have_relation("search", matching("{?q}"))

Constant Summary

Constants included from HalMatcherHelpers

HalMatcherHelpers::NullMatcher

Instance Attribute Summary

Attributes included from HalMatcherHelpers

#repr

Instance Method Summary collapse

Methods included from HalMatcherHelpers

#matcher?, #matcherize, #parse, #sentencize

Constructor Details

#initialize(link_rel, *args) ⇒ RelationMatcher

Signature

expect(doc).to have_relation link_rel
expect(doc).to have_relation link_rel, href_matcher
expect(doc).to have_relation link_rel, template_variables
expect(doc).to have_relation link_rel, template_variables, href_matcher


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rspec/hal/matchers/relation_matcher.rb', line 19

def initialize(link_rel, *args)
  @link_rel = link_rel.to_s

  @tmpl_vars, @expected = *if args.empty?
                             [{},NullMatcher]
                           elsif args.size > 1
                             args
                           elsif hashish? args.first
                             [args.first, NullMatcher]
                           else
                             [{}, args.first]
                           end
end

Instance Method Details

#descriptionObject



51
52
53
# File 'lib/rspec/hal/matchers/relation_matcher.rb', line 51

def description
  "have #{link_rel} link or embedded"
end

#failure_messageObject Also known as: failure_message_for_should



41
42
43
# File 'lib/rspec/hal/matchers/relation_matcher.rb', line 41

def failure_message
  sentencize "Expected `#{link_rel}` link or embedded", expected.description, "but found none"
end

#failure_message_when_negatedObject Also known as: failure_message_for_should_not



46
47
48
# File 'lib/rspec/hal/matchers/relation_matcher.rb', line 46

def failure_message_when_negated
  "Expected `#{link_rel}` link to be absent"
end

#matches?(jsonish) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/rspec/hal/matchers/relation_matcher.rb', line 33

def matches?(jsonish)
  repr = parse(jsonish)

  !repr.related_hrefs(link_rel, tmpl_vars){[]}
    .select{|it| expected === it }
    .empty?
end

#with_href(expected) ⇒ Object



55
56
57
# File 'lib/rspec/hal/matchers/relation_matcher.rb', line 55

def with_href(expected)
  self.class.new(link_rel, tmpl_vars, matcherize(expected))
end