11require 'json'
22require 'jsonapi_spec_helpers/version'
33require 'jsonapi_spec_helpers/helpers'
4+ require 'jsonapi_spec_helpers/string_helpers'
45require 'jsonapi_spec_helpers/payload'
56require 'jsonapi_spec_helpers/payload_sanitizer'
67require 'jsonapi_spec_helpers/errors'
@@ -19,7 +20,7 @@ def self.load_payloads!
1920 Dir [ Rails . root . join ( 'spec/payloads/**/*.rb' ) ] . each { |f | require f }
2021 end
2122
22- def assert_payload ( name , record , json , &blk )
23+ def assert_payload ( name , record , json , dasherized : false , &blk )
2324 unless payload = JsonapiSpecHelpers ::Payload . registry [ name ]
2425 raise "No payloads registered for '#{ name } '"
2526 end
@@ -32,10 +33,12 @@ def assert_payload(name, record, json, &blk)
3233 aggregate_failures "payload has correct key/values" do
3334 payload . keys . each_pair do |attribute , options |
3435 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
3637 unless options [ :allow_nil ]
3738 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 )
3942
4043 if options [ :type ]
4144 expect ( json [ attribute . to_s ] ) . to match_type ( attribute , options [ :type ] )
@@ -47,8 +50,9 @@ def assert_payload(name, record, json, &blk)
4750 payload . no_keys . each do |no_key |
4851 expect ( json ) . to_not have_payload_key ( no_key , { } )
4952 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
5256 unexpected_keys . reject! { |k | %w( id jsonapi_type ) . include? ( k ) }
5357 unexpected_keys . each do |key |
5458 expect ( key ) . to be_not_in_payload
0 commit comments