Skip to content

Commit 69dadbf

Browse files
committedFeb 15, 2020
Rename into have_jsonapi_attributes
RSpec core matchers already have a `have_attributes` matcher. The matcher is extended to check strictly the number of attributes. Closes #4
1 parent 6a71d0c commit 69dadbf

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed
 

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ Available matchers:
3636

3737
* `expect(document['data']).to have_id('12')`
3838
* `expect(document['data']).to have_type('users')`
39-
* `expect(document['data']).to have_attributes(:name, :email)`
39+
* `expect(document['data']).to have_jsonapi_attributes(:name, :email)`
40+
* `expect(document['data']).to have_jsonapi_attributes(:name, :email, :country).exactly`
4041
* `expect(document['data']).to have_attribute(:name).with_value('Lucas')`
4142
* `expect(document['data']).to have_relationships(:posts, :comments)`
4243
* `expect(document['data']).to have_relationship(:posts).with_data([{ 'id' => '1', 'type' => 'posts' }])`

‎lib/jsonapi/rspec/attributes.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@ module Attributes
1313
end
1414
end
1515

16-
::RSpec::Matchers.define :have_attributes do |*attrs|
16+
::RSpec::Matchers.define :have_jsonapi_attributes do |*attrs|
1717
match do |actual|
1818
return false unless actual.key?('attributes')
1919

20-
attrs.all? { |attr| actual['attributes'].key?(attr.to_s) }
20+
counted = (attrs.size == actual['attributes'].size) if @exactly
21+
22+
(attrs.map(&:to_s) - actual['attributes'].keys).empty? &&
23+
(counted == @exactly)
24+
end
25+
26+
chain :exactly do
27+
@exactly = true
2128
end
2229
end
2330
end

‎lib/jsonapi/rspec/relationships.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ module Relationships
1717
if !(actual['relationships'] || {}).key?(rel.to_s)
1818
"expected #{actual} to have relationship #{rel}"
1919
else
20-
"expected #{actual['relationships'][rel.to_s]} to have data #{@data_val}"
20+
"expected #{actual['relationships'][rel.to_s]} " \
21+
"to have data #{@data_val}"
2122
end
2223
end
2324
end

‎spec/jsonapi/attributes_spec.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe JSONAPI::RSpec do
4+
let(:doc) do
5+
{
6+
'attributes' => {
7+
'one' => 1,
8+
'two' => 2,
9+
'four' => 3
10+
}
11+
}
12+
end
13+
14+
describe '#have_attribute' do
15+
it { expect(doc).to have_attribute(:one) }
16+
it { expect(doc).not_to have_attribute(:five) }
17+
end
18+
19+
describe '#have_jsonapi_attributes' do
20+
it { expect(doc).to have_jsonapi_attributes(:one, :two) }
21+
it { expect(doc).not_to have_jsonapi_attributes(:two, :five) }
22+
it { expect(doc).to have_jsonapi_attributes(:one, :two, :four).exactly }
23+
it { expect(doc).not_to have_jsonapi_attributes(:one).exactly }
24+
end
25+
end

‎spec/jsonapi/jsonapi_object_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
RSpec.describe JSONAPI::RSpec, '#have_jsonapi_object' do
44
context 'when providing no value' do
55
it 'succeeds when jsonapi object is present' do
6-
expect('jsonapi' => { 'version' => '1.0'}).to have_jsonapi_object
6+
expect('jsonapi' => { 'version' => '1.0' }).to have_jsonapi_object
77
end
88

99
it 'fails when jsonapi object is absent' do

0 commit comments

Comments
 (0)
Please sign in to comment.