Sha256: 43184c754ba57413d984322a95b2b721de2f1f59048a7559420b28bc97c08192
Contents?: true
Size: 1.81 KB
Versions: 1
Compression:
Stored size: 1.81 KB
Contents
require 'rspec/matchers' require_relative 'string' # Class BeJsonApiResponse provides custom RSpec matching for json:api # responses in general. # # It expects a Rack::Response (or similar) response object # # Usage: # expect(response).to BeJsonApiResponse.new # # @author Chris Blackburn <87a1779b@opayq.com> # class BeJsonApiResponse include JsonapiRspec def matches?(response) return false unless valid_response?(response) @parsed_response = JSON.parse(response.body) return false if response_is_error? return false unless required_top_level_sections? return false if conflicting_sections? if JsonapiRspec.configuration.meta_required return false unless valid_meta_section? end @parsed_response.each_key do |key| case key.to_sym when :data return false unless valid_data_section? when :meta return false unless valid_meta_section? when :jsonapi next # this can legally be anything when :included next # TODO: handle included objects when :links next # TODO: handle links objects else return set_failure_message(FailureMessages::UNEXPECTED_TOP_LVL_KEY % key) end end true end private # Set the failure message # # @param [String] msg Failure message # # @return [Boolean] always returns false # def set_failure_message(msg) @failure_message = "#{FailureMessages::GENERAL_PREFIX} #{msg}" false end end # Usage: # expect(response).to be_jsonapi_response # RSpec::Matchers.define :be_jsonapi_response do match do |actual_response| @instance = BeJsonApiResponse.new def failure_message @instance.failure_message end def failure_message_when_negated @instance.failure_message end @instance.matches?(actual_response) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
jsonapi_rspec-0.2.2 | lib/jsonapi_rspec/be_json_api_response.rb |