1+ require 'rspec-api/matchers/attributes'
2+
3+ module RSpecApi
4+ module Matchers
5+ # Convert RSpecAPI::Matchers classes into RSpec-compatible matchers, e.g.:
6+ # makes RSpecApi::Matchers::Status available as expect(...).to match_status
7+ module DSL
8+ # Passes if response body is an object or array of objects with +key+.
9+ #
10+ # @example
11+ #
12+ # # Passes if the body is an object with key :id
13+ # body = '{"id": 1, "url": "http://www.example.com"}'
14+ # expect(OpenStruct.new body: body).to have_attributes(:id)
15+ #
16+ # # Passes if the body is an object with key :id and value 1
17+ # body = '{"id": 1, "url": "http://www.example.com"}'
18+ # expect(OpenStruct.new body: body).to have_attributes(id: {value: 1})
19+ #
20+ # # Passes if the body is an array of objects, all with key :id
21+ # body = '{"id": 1, "name": ""}, {"id": 2}]'
22+ # expect(OpenStruct.new body: body).to have_attributes(:id)
23+ #
24+ # # Passes if the body is an array of object with key :id and odd values
25+ # body = '{"id": 1, "name": ""}, {"id": 3}], {"id": 5}]'
26+ # expect(OpenStruct.new body: body).to have_attributes(id: {value: -> v {v.odd?}})
27+
28+ # TODO: add &block options
29+ def have_attributes ( attributes = { } )
30+ RSpecApi ::Matchers ::Attributes . new attributes
31+ end
32+ # alias have_attribute have_attributes
33+ # alias have_keys have_attributes
34+ # alias have_key have_attributes
35+ end
36+ end
37+ end
0 commit comments