Sha256: a09332bbad67d1e35d2df07f399112602dd6019a5c53213477182d4e456ff5d3
Contents?: true
Size: 1.67 KB
Versions: 12
Compression:
Stored size: 1.67 KB
Contents
module Shoulda # :nodoc: module Matchers module ActionController # :nodoc: # Ensures a controller responded with the expected json. def respond_with_json(status) RespondWithJsonMatcher.new(status) end class RespondWithJsonMatcher # :nodoc: def initialize(expected_json) @no_values = false @ignored = [] @expected = expected_json end def ignoring_attributes(ignored = ['id', 'updated_at', 'created_at']) @ignored = ignored self end def ignoring_values() @no_values = true self end def matches?(controller) @actual = controller.response.body # remove the selected attributes @ignored.each do |attr| @actual.gsub!(/.#{attr}.:[^{,]*[,]+/, "") @expected.gsub!(/.#{attr}.:[^{,]*[,]+/, "") end # remove the values of all attributes if @no_values @actual.gsub!(/:[^{,]*/, "") @expected.gsub!(/:[^{,]*/, "") end @actual == @expected end def failure_message s = "Expected the following json strings to be equal: \n" s += "Expected: " + @expected.inspect + "\n" s += " Got: " + @actual.inspect s end def negative_failure_message s = "Expected the following json strings to be different: \n" s += "Expected: " + @expected.inspect + "\n" s += " Got: " + @actual.inspect s end def description "respond with a defined json string" end end end end end
Version data entries
12 entries across 12 versions & 1 rubygems