jsonapi_spec_helpers 0.2.1 → 0.2.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6c612599d47e809e0d0af99c60fb425d87a7f04c
4
- data.tar.gz: 1482e1339767f47a9a10e8f3f12b7bd3a9bdad44
3
+ metadata.gz: c95f70b24431c804dc7e611071e2bfb15f702aff
4
+ data.tar.gz: daffe460ccef792159feb8379277e12d1af39fbe
5
5
  SHA512:
6
- metadata.gz: ca6adc718e8a9a28801b088f70499dd9885cd8f370559fd13ecf932e39ac69f373540c129eda6238f3e714bf30e294cb40e11c1ad9315446e584ba1cc5c891c8
7
- data.tar.gz: 42311907667ca7fc828e9be586460a0a89c652014094816963957bb5a50b5c33371967a8cc9176384f2c1d0ecd2ceb71b4a3214c173a04dff8c41b4b907d0ea5
6
+ metadata.gz: 1d6a4b7cad9ec9b7ccf07d9e187821f4e3cb31aa4c210b9350e86e4b8a98c712e858f8c4b399ddea37f47b9c10b68ea4ff2bb911a3c7792c0cc824856afc0d79
7
+ data.tar.gz: 22aeefed1fecb2bfe9eb69437154e66c0d7a54396edaaa336f710e79f04564293cf3f7ce39e27cc537a20459ebacbcecd933b3e58c950517263cdd7644793d4e
@@ -30,16 +30,17 @@ module JsonapiSpecHelpers
30
30
  @no_keys << name
31
31
  end
32
32
 
33
- def key(name, &blk)
33
+ def key(name, options = {}, &blk)
34
+ options[:allow_nil] ||= false
34
35
  @no_keys.reject! { |k| k == name }
35
36
  prc = blk
36
37
  prc = ->(record) { record.send(name) } if prc.nil?
37
- @keys[name] = prc
38
+ @keys[name] = options.merge(proc: prc)
38
39
  end
39
40
 
40
41
  def timestamps!
41
- @keys[:created_at] = ->(record) { record.created_at }
42
- @keys[:updated_at] = ->(record) { record.updated_at }
42
+ @keys[:created_at] = key(:created_at)
43
+ @keys[:updated_at] = key(:updated_at)
43
44
  end
44
45
  end
45
46
  end
@@ -1,3 +1,3 @@
1
1
  module JsonapiSpecHelpers
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -1,6 +1,3 @@
1
- require 'pry'
2
- require 'pry-byebug'
3
-
4
1
  require 'json'
5
2
  require 'jsonapi_spec_helpers/version'
6
3
  require 'jsonapi_spec_helpers/payload'
@@ -16,13 +13,21 @@ RSpec::Matchers.define :match_payload do |attribute, expected|
16
13
  end
17
14
  end
18
15
 
19
- RSpec::Matchers.define :have_payload_key do |expected|
16
+ RSpec::Matchers.define :have_payload_key do |expected, allow_nil|
20
17
  match do |json|
21
- json.has_key?(expected.to_s)
18
+ @has_key = json.has_key?(expected.to_s)
19
+ @has_value = !json[expected.to_s].nil?
20
+
21
+ if allow_nil
22
+ @has_key
23
+ else
24
+ @has_key && @has_value
25
+ end
22
26
  end
23
27
 
24
28
  failure_message do |actual|
25
- "Expected JSON payload to have key '#{expected}' but was not present"
29
+ msg = !allow_nil && @has_key ? "nil. Use 'key(:foo, allow_nil: true)' to allow nils" : "not present"
30
+ "Expected JSON payload to have key '#{expected}' but was #{msg}"
26
31
  end
27
32
 
28
33
  failure_message_when_negated do |actual|
@@ -113,14 +118,17 @@ module JsonapiSpecHelpers
113
118
  end
114
119
 
115
120
  aggregate_failures "payload has correct key/values" do
116
- payload.keys.each_pair do |attribute, prc|
117
- if (expect(json).to have_payload_key(attribute)) == true
118
- expect(json[attribute.to_s]).to match_payload(attribute, prc.call(record))
121
+ payload.keys.each_pair do |attribute, options|
122
+ prc = options[:proc]
123
+ if (expect(json).to have_payload_key(attribute, options[:allow_nil])) == true
124
+ unless options[:allow_nil]
125
+ expect(json[attribute.to_s]).to match_payload(attribute, prc.call(record))
126
+ end
119
127
  end
120
128
  end
121
129
 
122
130
  payload.no_keys.each do |no_key|
123
- expect(json).to_not have_payload_key(no_key)
131
+ expect(json).to_not have_payload_key(no_key, {})
124
132
  end
125
133
 
126
134
  unexpected_keys = json.keys - payload.keys.keys.map(&:to_s)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi_spec_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-23 00:00:00.000000000 Z
11
+ date: 2016-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -135,9 +135,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
135
  version: '0'
136
136
  requirements: []
137
137
  rubyforge_project:
138
- rubygems_version: 2.4.5.1
138
+ rubygems_version: 2.5.1
139
139
  signing_key:
140
140
  specification_version: 4
141
141
  summary: Spec helpers for jsonapi
142
142
  test_files: []
143
- has_rdoc: