jsonapi-rspec 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +26 -0
- data/lib/jsonapi/rspec/attributes.rb +17 -24
- data/lib/jsonapi/rspec/id.rb +1 -1
- data/lib/jsonapi/rspec/type.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 52b2400bcac651bfc0b4c8421d716c79fbaf2e18041a2eef042fe610ed07d6e9
|
4
|
+
data.tar.gz: 4222824ea957fe826a5d2e6fc7cf2bccfbe05abd012047809ea1e88ad75b426c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7aaf222387bac2360894059c1dea6b82e8c954518d6ed36aad4381784b40d08f93c12dea33d56c6eb07efaf7b8c2da37a40ef3ac68d37bbf0400aab2f173cc7d
|
7
|
+
data.tar.gz: 8322947de5e6a2832e1fa0813ce26a4a1db01f9b1fe7fc0236e9f66913f597386144454363cb184f252f178fe9001be86e6220e6ea94cef95313307aa06e2949
|
data/README.md
CHANGED
@@ -47,6 +47,32 @@ Available matchers:
|
|
47
47
|
* `expect(document).to have_jsonapi_object`
|
48
48
|
* `expect(document).to have_jsonapi_object('version' => '1.0')`
|
49
49
|
|
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
|
+
|
50
76
|
## Advanced examples
|
51
77
|
|
52
78
|
Checking for an included resource:
|
@@ -1,51 +1,44 @@
|
|
1
1
|
module JSONAPI
|
2
2
|
module RSpec
|
3
3
|
module Attributes
|
4
|
-
::RSpec::Matchers.define :have_attribute do |
|
5
|
-
match do |
|
6
|
-
|
7
|
-
|
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']
|
8
8
|
|
9
|
-
return false unless
|
9
|
+
return false unless attributes_node
|
10
10
|
|
11
|
-
@
|
12
|
-
|
13
|
-
|
11
|
+
@existing_attributes = attributes_node.keys
|
12
|
+
@has_attribute = attributes_node.key?(attr_name.to_s)
|
13
|
+
@actual = attributes_node[attr_name.to_s]
|
14
14
|
|
15
|
-
|
16
|
-
@actual = @actual_value
|
17
|
-
@expected = @expected_value
|
15
|
+
return @actual == @expected if @has_attribute && @should_match_value
|
18
16
|
|
19
|
-
return @actual == @expected
|
20
|
-
end
|
21
17
|
@has_attribute
|
22
18
|
end
|
23
19
|
|
24
20
|
chain :with_value do |expected_value|
|
25
21
|
@should_match_value = true
|
26
|
-
@
|
22
|
+
@expected = expected_value
|
27
23
|
end
|
28
24
|
|
29
25
|
description do
|
30
|
-
result = "have attribute #{
|
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
|
34
28
|
result
|
35
29
|
end
|
36
30
|
|
37
|
-
failure_message do |
|
38
|
-
if @
|
39
|
-
"expected `#{
|
31
|
+
failure_message do |_doc|
|
32
|
+
if @actual
|
33
|
+
"expected `#{attr_name}` attribute " \
|
40
34
|
"to have value `#{@expected}` but was `#{@actual}`"
|
41
35
|
else
|
42
|
-
"expected attributes to include `#{
|
43
|
-
"Actual attributes were #{@
|
36
|
+
"expected attributes to include `#{attr_name}`. " \
|
37
|
+
"Actual attributes were #{@existing_attributes}"
|
44
38
|
end
|
45
39
|
end
|
46
40
|
|
47
41
|
diffable
|
48
|
-
attr_reader :actual, :expected
|
49
42
|
end
|
50
43
|
|
51
44
|
::RSpec::Matchers.define :have_jsonapi_attributes do |*attrs|
|
data/lib/jsonapi/rspec/id.rb
CHANGED
data/lib/jsonapi/rspec/type.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-rspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lucas Hosseini
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-core
|