Skip to content

Commit 1a2a44a

Browse files
lukemeliastas
authored andcommitted
Add support for .exactly to the have_relationships(...) matcher
1 parent 03e81b2 commit 1a2a44a

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Available matchers:
3737
* `expect(document['data']).to have_jsonapi_attributes(:name, :email, :country).exactly`
3838
* `expect(document['data']).to have_attribute(:name).with_value('Lucas')`
3939
* `expect(document['data']).to have_relationships(:posts, :comments)`
40+
* `expect(document['data']).to have_relationships(:posts, :comments, :likes).exactly`
4041
* `expect(document['data']).to have_relationship(:posts).with_data([{ 'id' => '1', 'type' => 'posts' }])`
4142
* `expect(document['data']['relationships']['posts']).to have_links(:self, :related)`
4243
* `expect(document['data']).to have_link(:self).with_value('http://api.example.com/users/12')`

lib/jsonapi/rspec/relationships.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ module Relationships
2929
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
3030
return false unless actual.key?('relationships')
3131

32-
rels.map(&:to_s).all? { |rel| actual['relationships'].key?(rel) }
32+
counted = (rels.size == actual['relationships'].keys.size) if @exactly
33+
34+
rels.map(&:to_s).all? { |rel| actual['relationships'].key?(rel) } \
35+
&& (counted == @exactly)
36+
end
37+
38+
chain :exactly do
39+
@exactly = true
3340
end
3441
end
3542
end

spec/jsonapi/relationships_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
it { expect(doc).not_to have_relationship('authors') }
2424
it { expect(doc).to have_relationship('user') }
2525

26+
it { expect(doc).to have_relationships('user', 'comments').exactly }
27+
it { expect(doc).not_to have_relationships('comments').exactly }
28+
2629
it do
2730
expect(doc).to have_relationship('user').with_data(
2831
{ 'id' => '1', 'type' => 'user' }

0 commit comments

Comments
 (0)