Skip to content

Commit 56226ed

Browse files
committed
Fix matcher args consitency based on JSON:API spec. Closes #19
1 parent b303724 commit 56226ed

File tree

5 files changed

+46
-27
lines changed

5 files changed

+46
-27
lines changed

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ Style/IfUnlessModifier:
1515
Enabled: false
1616

1717
Metrics/BlockLength:
18-
Max: 35
18+
Max: 30
1919
Exclude:
2020
- 'spec/*/*_spec.rb'

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,32 @@ Available matchers:
4747
* `expect(document).to have_jsonapi_object`
4848
* `expect(document).to have_jsonapi_object('version' => '1.0')`
4949

50+
### On matcher arguments...
51+
52+
**Note**: JSON:API spec requires JSON documents, thus attribute, relationship
53+
and link matcher arguments will always be converted into strings for
54+
consistency!!!
55+
56+
Basically, the tests bellow are absolutely equal:
57+
58+
```ruby
59+
expect(document['data']).to have_id(12)
60+
expect(document['data']).to have_id('12')
61+
62+
expect(document['data']).to have_type(:users)
63+
expect(document['data']).to have_type('users')
64+
65+
expect(document['data']).to have_jsonapi_attributes(:name, :email)
66+
expect(document['data']).to have_jsonapi_attributes('name', 'email')
67+
```
68+
69+
The JSON:API spec also requires the `id` and `type` to be strings, so any other
70+
argument passed will also be converted into a string.
71+
72+
If the document you are trying to test has mixed string/symbol keys, just
73+
configure matchers to be indifferent in that regard, using the
74+
`jsonapi_indifferent_hash = true` configuration option.
75+
5076
## Advanced examples
5177

5278
Checking for an included resource:

lib/jsonapi/rspec/attributes.rb

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,44 @@
11
module JSONAPI
22
module RSpec
33
module Attributes
4-
::RSpec::Matchers.define :have_attribute do |attr|
5-
match do |actual|
6-
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
7-
@attributes_node = actual['attributes']
4+
::RSpec::Matchers.define :have_attribute do |attr_name|
5+
match do |doc|
6+
doc = JSONAPI::RSpec.as_indifferent_hash(doc)
7+
attributes_node = doc['attributes']
88

9-
return false unless @attributes_node
9+
return false unless attributes_node
1010

11-
@has_attribute = @attributes_node.key?(attr.to_s)
12-
if @has_attribute && @should_match_value
13-
@actual_value = @attributes_node[attr.to_s]
11+
@existing_attributes = attributes_node.keys
12+
@has_attribute = attributes_node.key?(attr_name.to_s)
13+
@actual = attributes_node[attr_name.to_s]
1414

15-
# Work nicely with diffable
16-
@actual = @actual_value
17-
@expected = @expected_value
15+
return @actual == @expected if @has_attribute && @should_match_value
1816

19-
return @actual == @expected
20-
end
2117
@has_attribute
2218
end
2319

2420
chain :with_value do |expected_value|
2521
@should_match_value = true
26-
@expected_value = expected_value
22+
@expected = expected_value
2723
end
2824

2925
description do
30-
result = "have attribute #{attr.inspect}"
31-
if @should_match_value
32-
result << " with value #{@expected_value.inspect}"
33-
end
26+
result = "have attribute #{attr_name.inspect}"
27+
result << " with value #{@expected.inspect}" if @should_match_value
3428
result
3529
end
3630

37-
failure_message do |_actual|
38-
if @has_attribute
39-
"expected `#{attr}` attribute " \
31+
failure_message do |_doc|
32+
if @actual
33+
"expected `#{attr_name}` attribute " \
4034
"to have value `#{@expected}` but was `#{@actual}`"
4135
else
42-
"expected attributes to include `#{attr}`. " \
43-
"Actual attributes were #{@attributes_node.keys}"
36+
"expected attributes to include `#{attr_name}`. " \
37+
"Actual attributes were #{@existing_attributes}"
4438
end
4539
end
4640

4741
diffable
48-
attr_reader :actual, :expected
4942
end
5043

5144
::RSpec::Matchers.define :have_jsonapi_attributes do |*attrs|

lib/jsonapi/rspec/id.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module RSpec
33
module Id
44
::RSpec::Matchers.define :have_id do |expected|
55
match do |actual|
6-
JSONAPI::RSpec.as_indifferent_hash(actual)['id'] == expected
6+
JSONAPI::RSpec.as_indifferent_hash(actual)['id'] == expected.to_s
77
end
88
end
99
end

lib/jsonapi/rspec/type.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module RSpec
33
module Type
44
::RSpec::Matchers.define :have_type do |expected|
55
match do |actual|
6-
JSONAPI::RSpec.as_indifferent_hash(actual)['type'] == expected
6+
JSONAPI::RSpec.as_indifferent_hash(actual)['type'] == expected.to_s
77
end
88
end
99
end

0 commit comments

Comments
 (0)