Skip to content

Commit bdc73ee

Browse files
sa73917stas
authored andcommitted
Incorporate as_indifferent_hash into all matchers
Start each matcher with a call to as_indifferent_hash to return a string keyed (or indifferent access) version of the source document.
1 parent 6377fea commit bdc73ee

File tree

7 files changed

+14
-2
lines changed

7 files changed

+14
-2
lines changed

lib/jsonapi/rspec/attributes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module RSpec
33
module Attributes
44
::RSpec::Matchers.define :have_attribute do |attr|
55
match do |actual|
6+
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
67
(actual['attributes'] || {}).key?(attr.to_s) &&
78
(!@val_set || actual['attributes'][attr.to_s] == @val)
89
end
@@ -15,6 +16,7 @@ module Attributes
1516

1617
::RSpec::Matchers.define :have_jsonapi_attributes do |*attrs|
1718
match do |actual|
19+
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
1820
return false unless actual.key?('attributes')
1921

2022
counted = (attrs.size == actual['attributes'].size) if @exactly

lib/jsonapi/rspec/id.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ module JSONAPI
22
module RSpec
33
module Id
44
::RSpec::Matchers.define :have_id do |expected|
5-
match { |actual| actual['id'] == expected }
5+
match do |actual|
6+
JSONAPI::RSpec.as_indifferent_hash(actual)['id'] == expected
7+
end
68
end
79
end
810
end

lib/jsonapi/rspec/jsonapi_object.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module RSpec
33
module JsonapiObject
44
::RSpec::Matchers.define :have_jsonapi_object do |val|
55
match do |actual|
6+
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
67
actual.key?('jsonapi') &&
78
(!val || actual['jsonapi'] == val)
89
end

lib/jsonapi/rspec/links.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module RSpec
33
module Links
44
::RSpec::Matchers.define :have_link do |link|
55
match do |actual|
6+
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
67
actual.key?('links') && actual['links'].key?(link.to_s) &&
78
(!@val_set || actual['links'][link.to_s] == @val)
89
end
@@ -15,6 +16,7 @@ module Links
1516

1617
::RSpec::Matchers.define :have_links do |*links|
1718
match do |actual|
19+
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
1820
return false unless actual.key?('links')
1921

2022
links.all? { |link| actual['links'].key?(link.to_s) }

lib/jsonapi/rspec/meta.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module RSpec
33
module Meta
44
::RSpec::Matchers.define :have_meta do |val|
55
match do |actual|
6+
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
67
actual.key?('meta') &&
78
(!val || actual['meta'] == val)
89
end

lib/jsonapi/rspec/relationships.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module RSpec
33
module Relationships
44
::RSpec::Matchers.define :have_relationship do |rel|
55
match do |actual|
6+
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
67
return false unless (actual['relationships'] || {}).key?(rel.to_s)
78

89
!@data_set || actual['relationships'][rel.to_s]['data'] == @data_val
@@ -25,6 +26,7 @@ module Relationships
2526

2627
::RSpec::Matchers.define :have_relationships do |*rels|
2728
match do |actual|
29+
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
2830
return false unless actual.key?('relationships')
2931

3032
rels.all? { |rel| actual['relationships'].key?(rel) }

lib/jsonapi/rspec/type.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ module JSONAPI
22
module RSpec
33
module Type
44
::RSpec::Matchers.define :have_type do |expected|
5-
match { |actual| actual['type'] == expected }
5+
match do |actual|
6+
JSONAPI::RSpec.as_indifferent_hash(actual)['type'] == expected
7+
end
68
end
79
end
810
end

0 commit comments

Comments
 (0)