Module: RSpecApi::Matchers::Attributes

Included in:
RSpecApi::Matchers
Defined in:
lib/rspec-api/matchers/attributes/matcher.rb,
lib/rspec-api/matchers/attributes/have_attributes.rb

Defined Under Namespace

Classes: Matcher

Instance Method Summary collapse

Instance Method Details

#have_attributes(*attributes) ⇒ Object

Passes if the object includes the expected attributes in the body.

The method works only if the object is a JSON object or a collection of JSON objects; in the latter case all the objects need to include the expected attributes for the expectation to pass.

Examples:

Passes if the body is a collection of objects with a URL site

require 'rspec-api-matchers'

body = '[{"site": "http://www.foo.com"}, {"site": "http://bar.com"}]'
obj = OpenStruct.new body: body

describe 'have_attributes' do
  include RSpecApi::Matchers::Attributes
  it { expect(obj).to have_attributes(site: {type: {string: :url}}) }
end

# => (rspec) 1 example, 0 failures

Parameters:

  • attributes (Hash)

    The expected attributes in the body

See Also:



28
29
30
# File 'lib/rspec-api/matchers/attributes/have_attributes.rb', line 28

def have_attributes(*attributes)
  RSpecApi::Matchers::Attributes::Matcher.new *attributes
end