jsonapi-rspec 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5cb345693bd692513446e88fa4d1713b7927afa9b3a3033270ac26daefe3c07f
4
- data.tar.gz: 8bc079f20dc7cd85e29b2a51f2d44e511d09153809507dbc4f361b55d79caf8e
3
+ metadata.gz: 52b2400bcac651bfc0b4c8421d716c79fbaf2e18041a2eef042fe610ed07d6e9
4
+ data.tar.gz: 4222824ea957fe826a5d2e6fc7cf2bccfbe05abd012047809ea1e88ad75b426c
5
5
  SHA512:
6
- metadata.gz: 15c13b39490b4a72ef66e1aeed229665aacf8b98a0b3f2dfc8063d4b9dfa18190edb2c9a783cf921f23c0f5553d408065626669f4c39b7fa4cbdfb991e2e559e
7
- data.tar.gz: e82b0d326630160bf704058b38068de72521670715280fbdf1539d17b725cb1e721167ff5e2ed830f73baa45bf100ed1f999589ef7392a0b19254bbb500b87f3
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 |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']
8
8
 
9
- return false unless @attributes_node
9
+ return false unless attributes_node
10
10
 
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]
14
14
 
15
- # Work nicely with diffable
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
- @expected_value = expected_value
22
+ @expected = expected_value
27
23
  end
28
24
 
29
25
  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
34
28
  result
35
29
  end
36
30
 
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 " \
40
34
  "to have value `#{@expected}` but was `#{@actual}`"
41
35
  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}"
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|
@@ -3,7 +3,7 @@ module JSONAPI
3
3
  module Id
4
4
  ::RSpec::Matchers.define :have_id do |expected|
5
5
  match do |actual|
6
- JSONAPI::RSpec.as_indifferent_hash(actual)['id'] == expected
6
+ JSONAPI::RSpec.as_indifferent_hash(actual)['id'] == expected.to_s
7
7
  end
8
8
  end
9
9
  end
@@ -3,7 +3,7 @@ module JSONAPI
3
3
  module Type
4
4
  ::RSpec::Matchers.define :have_type do |expected|
5
5
  match do |actual|
6
- JSONAPI::RSpec.as_indifferent_hash(actual)['type'] == expected
6
+ JSONAPI::RSpec.as_indifferent_hash(actual)['type'] == expected.to_s
7
7
  end
8
8
  end
9
9
  end
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.7
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-25 00:00:00.000000000 Z
11
+ date: 2020-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-core