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 Attribute Summary

Attributes included from HalMatcherHelpers

#repr

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.to_s
  @expected = matcherize expected
end

Instance Method Details

#descriptionObject



35
36
37
# File 'lib/rspec/hal/matchers/have_property_matcher.rb', line 35

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

#failure_messageObject Also known as: failure_message_for_should



25
26
27
# File 'lib/rspec/hal/matchers/have_property_matcher.rb', line 25

def failure_message
  sentencize "Expected property `#{prop_name}`", expected.description
end

#failure_message_when_negatedObject Also known as: failure_message_for_should_not



30
31
32
# File 'lib/rspec/hal/matchers/have_property_matcher.rb', line 30

def failure_message_when_negated
  sentencize "Expected property `#{prop_name}`", expected.description, "to be absent"
end

#including(expected) ⇒ Object



45
46
47
# File 'lib/rspec/hal/matchers/have_property_matcher.rb', line 45

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)
  self.repr = jsonish

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

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



40
41
42
# File 'lib/rspec/hal/matchers/have_property_matcher.rb', line 40

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