File tree 5 files changed +39
-5
lines changed
5 files changed +39
-5
lines changed Original file line number Diff line number Diff line change @@ -36,7 +36,8 @@ Available matchers:
36
36
37
37
* ` expect(document['data']).to have_id('12') `
38
38
* ` 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 `
40
41
* ` expect(document['data']).to have_attribute(:name).with_value('Lucas') `
41
42
* ` expect(document['data']).to have_relationships(:posts, :comments) `
42
43
* ` expect(document['data']).to have_relationship(:posts).with_data([{ 'id' => '1', 'type' => 'posts' }]) `
Original file line number Diff line number Diff line change @@ -13,11 +13,18 @@ module Attributes
13
13
end
14
14
end
15
15
16
- ::RSpec ::Matchers . define :have_attributes do |*attrs |
16
+ ::RSpec ::Matchers . define :have_jsonapi_attributes do |*attrs |
17
17
match do |actual |
18
18
return false unless actual . key? ( 'attributes' )
19
19
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
21
28
end
22
29
end
23
30
end
Original file line number Diff line number Diff line change @@ -17,7 +17,8 @@ module Relationships
17
17
if !( actual [ 'relationships' ] || { } ) . key? ( rel . to_s )
18
18
"expected #{ actual } to have relationship #{ rel } "
19
19
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 } "
21
22
end
22
23
end
23
24
end
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change 3
3
RSpec . describe JSONAPI ::RSpec , '#have_jsonapi_object' do
4
4
context 'when providing no value' do
5
5
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
7
7
end
8
8
9
9
it 'fails when jsonapi object is absent' do
You can’t perform that action at this time.
0 commit comments