Skip to content

Commit 7bfbb36

Browse files
committed
Initial commit.
1 parent 3b4f92e commit 7bfbb36

12 files changed

+232
-0
lines changed

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec

README.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# jsonapi-rspec
2+
3+
RSpec matchers for [JSON API](http://jsonapi.org).
4+
5+
## Status
6+
7+
[![Gem Version](https://badge.fury.io/rb/jsonapi-rspec.svg)](https://badge.fury.io/rb/jsonapi-rspec)
8+
[![Build Status](https://secure.travis-ci.org/jsonapi-rb/jsonapi-rspec.svg?branch=master)](http://travis-ci.org/jsonapi-rb/jsonapi-rspec?branch=master)
9+
[![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](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).

VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.0.1

jsonapi-rspec.gemspec

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version = File.read(File.expand_path('../VERSION', __FILE__)).strip
2+
3+
Gem::Specification.new do |spec|
4+
spec.name = 'jsonapi-rspec'
5+
spec.version = version
6+
spec.author = ['Lucas Hosseini']
7+
spec.email = ['[email protected]']
8+
spec.summary = 'RSpec matchers for JSON API.'
9+
spec.description = 'Helpers for validating JSON API payloads'
10+
spec.homepage = 'https://github.com/jsonapi-rb/jsonapi-rspec'
11+
spec.license = 'MIT'
12+
13+
spec.files = Dir['README.md', 'lib/**/*']
14+
spec.require_path = 'lib'
15+
16+
spec.add_development_dependency 'rspec'
17+
spec.add_development_dependency 'rake'
18+
end

lib/jsonapi/rspec.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'jsonapi/rspec/id'
2+
require 'jsonapi/rspec/type'
3+
require 'jsonapi/rspec/attributes'
4+
require 'jsonapi/rspec/relationships'
5+
require 'jsonapi/rspec/links'
6+
require 'jsonapi/rspec/meta'
7+
require 'jsonapi/rspec/jsonapi_object'
8+
9+
module JSONAPI
10+
module RSpec
11+
include Id
12+
include Type
13+
include Attributes
14+
include Relationships
15+
include Links
16+
include Meta
17+
include JsonapiObject
18+
end
19+
end

lib/jsonapi/rspec/attributes.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module JSONAPI
2+
module RSpec
3+
module Attributes
4+
::RSpec::Matchers.define :have_attribute do |attr|
5+
match do |actual|
6+
(actual['attributes'] || {}).key?(attr.to_s) &&
7+
(!@val_set || actual['attributes'][attr.to_s] == @val)
8+
end
9+
10+
chain :with_value do |val|
11+
@val_set = true
12+
@val = val
13+
end
14+
end
15+
16+
::RSpec::Matchers.define :have_attributes do |*attrs|
17+
match do |actual|
18+
return false unless actual.key?('attributes')
19+
20+
attrs.all? { |attr| actual['attributes'].key?(attr.to_s) }
21+
end
22+
end
23+
end
24+
end
25+
end

lib/jsonapi/rspec/id.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module JSONAPI
2+
module RSpec
3+
module Id
4+
::RSpec::Matchers.define :have_id do |expected|
5+
match { |actual| actual['id'] == expected }
6+
end
7+
end
8+
end
9+
end

lib/jsonapi/rspec/jsonapi_object.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module JSONAPI
2+
module RSpec
3+
module JsonapiObject
4+
::RSpec::Matchers.define :have_jsonapi_object do |val|
5+
match do |actual|
6+
actual.key?('jsonapi') &&
7+
(!val || actual['jsonapi'] == val)
8+
end
9+
end
10+
end
11+
end
12+
end

lib/jsonapi/rspec/links.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module JSONAPI
2+
module RSpec
3+
module Links
4+
::RSpec::Matchers.define :have_link do |link|
5+
match do |actual|
6+
actual.key?('links') && actual['links'].key?(link.to_s) &&
7+
(!@val_set || actual['links'] == @val)
8+
end
9+
10+
chain :with_value do |val|
11+
@val_set = true
12+
@val = val
13+
end
14+
end
15+
16+
::RSpec::Matchers.define :have_links do |*links|
17+
match do |actual|
18+
return false unless actual.key?('links')
19+
20+
links.all? { |link| actual['links'].key?(link.to_s) }
21+
end
22+
end
23+
end
24+
end
25+
end

lib/jsonapi/rspec/meta.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module JSONAPI
2+
module RSpec
3+
module Meta
4+
::RSpec::Matchers.define :have_meta do |val|
5+
match do |actual|
6+
actual.key?('meta') &&
7+
(!val || actual['meta'] == val)
8+
end
9+
end
10+
end
11+
end
12+
end

lib/jsonapi/rspec/relationships.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module JSONAPI
2+
module RSpec
3+
module Relationships
4+
::RSpec::Matchers.define :have_relationship do |rel|
5+
match do |actual|
6+
return false unless (actual['relationships'] || {}).key?(rel.to_s)
7+
8+
!@data_set || actual['relationships'][rel.to_s]['data'] == @data_val
9+
end
10+
11+
chain :with_data do |val|
12+
@data_set = true
13+
@data_val = val
14+
end
15+
16+
failure_message do |actual|
17+
if !(actual['relationships'] || {}).key?(rel.to_s)
18+
"expected #{actual} to have relationship #{rel}"
19+
else
20+
"expected #{actual['relationships'][rel.to_s]} to have data #{@data_val}"
21+
end
22+
end
23+
end
24+
25+
::RSpec::Matchers.define :have_relationships do |*rels|
26+
match do |actual|
27+
return false unless actual.key?('relationships')
28+
29+
rels.all? { |rel| actual['relationships'].key?(rel) }
30+
end
31+
end
32+
end
33+
end
34+
end

lib/jsonapi/rspec/type.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module JSONAPI
2+
module RSpec
3+
module Type
4+
::RSpec::Matchers.define :have_type do |expected|
5+
match { |actual| actual['type'] == expected }
6+
end
7+
end
8+
end
9+
end

0 commit comments

Comments
 (0)