|
| 1 | +# jsonapi-rspec |
| 2 | + |
| 3 | +RSpec matchers for [JSON API](http://jsonapi.org). |
| 4 | + |
| 5 | +## Status |
| 6 | + |
| 7 | +[](https://badge.fury.io/rb/jsonapi-rspec) |
| 8 | +[](http://travis-ci.org/jsonapi-rb/jsonapi-rspec?branch=master) |
| 9 | +[](https://gitter.im/jsonapi-rb/Lobby) |
| 10 | + |
| 11 | +## Resources |
| 12 | + |
| 13 | +* Chat: [gitter](http://gitter.im/jsonapi-rb) |
| 14 | +* Twitter: [@jsonapirb](http://twitter.com/jsonapirb) |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +Add the following to your application's Gemfile: |
| 19 | +```ruby |
| 20 | +gem 'jsonapi-rspec' |
| 21 | +``` |
| 22 | +And then execute: |
| 23 | +``` |
| 24 | +$ bundle |
| 25 | +``` |
| 26 | + |
| 27 | +Add to your `spec/spec_helpers.rb`: |
| 28 | + |
| 29 | +```ruby |
| 30 | +# spec/spec_helpers.rb |
| 31 | +RSpec.configure do |config| |
| 32 | + # ... |
| 33 | + config.include JSONAPI::RSpec |
| 34 | +end |
| 35 | +``` |
| 36 | + |
| 37 | +## Usage and documentation |
| 38 | + |
| 39 | +Available matchers: |
| 40 | + |
| 41 | +* `expect(document['data']).to have_id('12')` |
| 42 | +* `expect(document['data']).to have_type('users')` |
| 43 | +* `expect(document['data']).to have_attributes(:name, :email)` |
| 44 | +* `expect(document['data']).to have_attribute(:name).with_value('Lucas')` |
| 45 | +* `expect(document['data']).to have_relationships(:posts, :comments)` |
| 46 | +* `expect(document['data']).to have_relationship(:posts).with_data([{ 'id' => '1', 'type' => 'posts' }])` |
| 47 | +* `expect(document['data']['relationships']['posts']).to have_links(:self, :related)` |
| 48 | +* `expect(document['data']).to have_link(:self).with_value('http://api.example.com/users/12')` |
| 49 | +* `expect(document).to have_meta` |
| 50 | +* `expect(document).to have_meta('foo' => 'bar')` |
| 51 | +* `expect(document).to have_jsonapi_object` |
| 52 | +* `expect(document).to have_jsonapi_object('version' => '1.0')` |
| 53 | + |
| 54 | +## Advanced examples |
| 55 | + |
| 56 | +Checking for an included resource: |
| 57 | + |
| 58 | +```ruby |
| 59 | +expect(response_body['included']) |
| 60 | + .to include(have_type('posts').and have_id('1')) |
| 61 | +``` |
| 62 | + |
| 63 | +## License |
| 64 | + |
| 65 | +jsonapi-rspec is released under the [MIT License](http://www.opensource.org/licenses/MIT). |
0 commit comments