class ShouldaRenderJsonObjectMatcher
Attributes
context[R]
failure_message[R]
keys[R]
negative_failure_message[R]
options[R]
response[R]
root[R]
Public Class Methods
new(root, options = {})
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 4 def initialize(root, options = {}) @options = options # Coercing types root: Symbol, keys: Array of Symbols @root = root.to_s @keys = { required: massage_options(:required), forbidden: massage_options(:forbidden), } end
Public Instance Methods
description()
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 14 def description "render JSON Hash/Object for #{@root}" end
matches?(controller)
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 18 def matches?(controller) @response = controller.response matches_content_type? && matches_root? && has_required_keys? && has_no_forbidden_keys? rescue JSON::ParserError set_failure_message "Response body was not a valid JSON string" false end
Private Instance Methods
expected_root_class()
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 87 def expected_root_class Hash end
forbidden_keys()
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 67 def forbidden_keys @forbidden_keys ||= keys[:forbidden].select{|key| present?(key)} end
format_keys(keys)
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 79 def format_keys(keys) keys.map{|key| "'#{key}'"}.join(', ') end
has_no_forbidden_keys?()
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 56 def has_no_forbidden_keys? return true unless keys[:forbidden] set_failure_message "Expected no child nodes for #{format_keys(forbidden_keys)} but they were present" # Negative failure here makes no sense since they are expected to not be present forbidden_keys.empty? end
has_required_keys?()
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 49 def has_required_keys? return true unless keys[:required] set_failure_message "Expected child nodes for #{format_keys(missing_keys)} but there were none" set_negative_failure_message "Expected no child nodes for #{format_keys(missing_keys)} but they were present" missing_keys.empty? end
json()
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 83 def json @json ||= JSON.parse(response.body) end
massage_options(key)
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 91 def massage_options(key) [options[key]].flatten.compact.map(&:to_s) end
matches_content_type?()
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 31 def matches_content_type? content_type = response.content_type set_failure_message "Expected content type to be 'application/json' but was '#{content_type}' instead" set_negative_failure_message "Expected content type not to be '#{content_type}'" content_type == 'application/json' end
matches_root?()
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 42 def matches_root? klass = json[root].class set_failure_message "Expected root level node '#{root}' as Hash/Object but it was not found [class was #{klass}]" set_negative_failure_message "Did not expect to contain root level node '#{root}' but it was present as #{klass}" json.has_key?(root) && json[root].is_a?(expected_root_class) end
missing_keys()
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 63 def missing_keys @missing_keys ||= keys[:required].reject{|key| present?(key)} end
present?(key)
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 95 def present?(key) json[root].has_key?(key) end
root_matches_expected_type?()
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 38 def root_matches_expected_type? matches_root? ? root_is_expected_type? : false end
set_failure_message(message)
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 75 def set_failure_message(message) @failure_message = message end
set_negative_failure_message(message)
click to toggle source
# File lib/shoulda_render_json_object_matcher.rb, line 71 def set_negative_failure_message(message) @negative_failure_message = message end