Class: RSpec::Hal::Matchers::HavePropertyMatcher

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

Overview

Example

expect(doc).to have_property("name")
expect(doc).to have_property("search", matching("Alice"))

Constant Summary

Constants included from HalMatcherHelpers

RSpec::Hal::Matchers::HalMatcherHelpers::NullMatcher

Instance Method Summary collapse

Methods included from HalMatcherHelpers

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

Constructor Details

#initialize(property_name, expected = NullMatcher) ⇒ HavePropertyMatcher

Returns a new instance of HavePropertyMatcher.



13
14
15
16
# File 'lib/rspec/hal/matchers/have_property_matcher.rb', line 13

def initialize(property_name, expected=NullMatcher)
  @prop_name = property_name
  @expected = expected
end

Instance Method Details

#descriptionObject



48
49
50
# File 'lib/rspec/hal/matchers/have_property_matcher.rb', line 48

def description
  "have property `#{prop_name}` #{@expected.description}"
end

#failure_messageObject Also known as: failure_message_for_should



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rspec/hal/matchers/have_property_matcher.rb', line 25

def failure_message
  but_clause = if outcome == :no_templates
                 "found only non-templated links"
               else
                 "found none"
               end
  expected_clause = if expected != NullMatcher && expected.respond_to?(:description)
                      expected.description
                    elsif expected != NullMatcher
                      "matching #{expected}"
                    else
                      "to exist"
                    end

  "Expected templated `#{link_rel}` link #{expected_clause} but #{but_clause}"
end

#failure_message_when_negatedObject Also known as: failure_message_for_should_not



43
44
45
# File 'lib/rspec/hal/matchers/have_property_matcher.rb', line 43

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

#including(expected) ⇒ Object



58
59
60
# File 'lib/rspec/hal/matchers/have_property_matcher.rb', line 58

def including(expected)
  self.class.new(prop_name, RSpec::Matchers::BuiltIn::Include.new(expected))
end

#matches?(jsonish) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
# File 'lib/rspec/hal/matchers/have_property_matcher.rb', line 18

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

  repr.property?(prop_name) &&
    expected === repr.property(prop_name){ nil }
end

#matching(expected) ⇒ Object Also known as: that_is



53
54
55
# File 'lib/rspec/hal/matchers/have_property_matcher.rb', line 53

def matching(expected)
  self.class.new(prop_name, matcherize(expected))
end