Module: JsonapiSpecHelpers
- Defined in:
- lib/jsonapi_spec_helpers.rb,
lib/jsonapi_spec_helpers/errors.rb,
lib/jsonapi_spec_helpers/helpers.rb,
lib/jsonapi_spec_helpers/payload.rb,
lib/jsonapi_spec_helpers/version.rb,
lib/jsonapi_spec_helpers/payload_sanitizer.rb
Defined Under Namespace
Modules: Errors, Helpers
Classes: Payload, PayloadSanitizer
Constant Summary
collapse
- VERSION =
"0.4.8"
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(klass) ⇒ Object
9
10
11
12
13
14
15
16
|
# File 'lib/jsonapi_spec_helpers.rb', line 9
def self.included(klass)
require 'jsonapi_spec_helpers/matchers'
klass.send(:include, Helpers)
if defined?(Rails)
load_payloads!
end
end
|
.load_payloads! ⇒ Object
18
19
20
|
# File 'lib/jsonapi_spec_helpers.rb', line 18
def self.load_payloads!
Dir[Rails.root.join('spec/payloads/**/*.rb')].each { |f| require f }
end
|
Instance Method Details
#assert_payload(name, record, json, &blk) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/jsonapi_spec_helpers.rb', line 22
def assert_payload(name, record, json, &blk)
unless payload = JsonapiSpecHelpers::Payload.registry[name]
raise "No payloads registered for '#{name}'"
end
if blk
payload = payload.fork
payload.instance_eval(&blk)
end
aggregate_failures "payload has correct key/values" do
payload.keys.each_pair do |attribute, options|
prc = options[:proc]
if (expect(json).to have_payload_key(attribute, options[:allow_nil])) == true
unless options[:allow_nil]
output = instance_exec(record, &prc)
expect(json[attribute.to_s]).to match_payload(attribute, output)
if options[:type]
expect(json[attribute.to_s]).to match_type(attribute, options[:type])
end
end
end
end
payload.no_keys.each do |no_key|
expect(json).to_not have_payload_key(no_key, {})
end
unexpected_keys = json.keys - payload.keys.keys.map(&:to_s)
unexpected_keys.reject! { |k| %w(id jsonapi_type).include?(k) }
unexpected_keys.each do |key|
expect(key).to be_not_in_payload
end
end
end
|