1
1
require 'json'
2
2
require 'jsonapi_spec_helpers/version'
3
3
require 'jsonapi_spec_helpers/helpers'
4
+ require 'jsonapi_spec_helpers/string_helpers'
4
5
require 'jsonapi_spec_helpers/payload'
5
6
require 'jsonapi_spec_helpers/payload_sanitizer'
6
7
require 'jsonapi_spec_helpers/errors'
@@ -19,7 +20,7 @@ def self.load_payloads!
19
20
Dir [ Rails . root . join ( 'spec/payloads/**/*.rb' ) ] . each { |f | require f }
20
21
end
21
22
22
- def assert_payload ( name , record , json , &blk )
23
+ def assert_payload ( name , record , json , dasherized : false , &blk )
23
24
unless payload = JsonapiSpecHelpers ::Payload . registry [ name ]
24
25
raise "No payloads registered for '#{ name } '"
25
26
end
@@ -32,10 +33,12 @@ def assert_payload(name, record, json, &blk)
32
33
aggregate_failures "payload has correct key/values" do
33
34
payload . keys . each_pair do |attribute , options |
34
35
prc = options [ :proc ]
35
- if ( expect ( json ) . to have_payload_key ( attribute , options [ :allow_nil ] ) ) == true
36
+ if ( expect ( json ) . to have_payload_key ( attribute , options [ :allow_nil ] , dasherized ) ) == true
36
37
unless options [ :allow_nil ]
37
38
output = instance_exec ( record , &prc )
38
- expect ( json [ attribute . to_s ] ) . to match_payload ( attribute , output )
39
+ attribute = attribute . to_s
40
+ attribute = StringHelpers . dasherize ( attribute ) if dasherized
41
+ expect ( json [ attribute ] ) . to match_payload ( attribute , output )
39
42
40
43
if options [ :type ]
41
44
expect ( json [ attribute . to_s ] ) . to match_type ( attribute , options [ :type ] )
@@ -47,8 +50,9 @@ def assert_payload(name, record, json, &blk)
47
50
payload . no_keys . each do |no_key |
48
51
expect ( json ) . to_not have_payload_key ( no_key , { } )
49
52
end
50
-
51
- unexpected_keys = json . keys - payload . keys . keys . map ( &:to_s )
53
+ unexpected_keys = json . keys - payload . keys . keys . map do |key |
54
+ dasherized ? StringHelpers . dasherize ( key ) : key . to_s
55
+ end
52
56
unexpected_keys . reject! { |k | %w( id jsonapi_type ) . include? ( k ) }
53
57
unexpected_keys . each do |key |
54
58
expect ( key ) . to be_not_in_payload
0 commit comments