Created
October 29, 2014 01:08
-
-
Save halostatue/98089647505a8221cfc8 to your computer and use it in GitHub Desktop.
expect(response).to be_json_for Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ContentTypeMatchers | |
extend RSpec::Matchers::DSL | |
matcher :be_content_type do |expected| | |
match do |actual| | |
expect(actual.content_type).to eq(expected) | |
end | |
failure_message_for_should do |actual| | |
%Q(expected that response content type "#{actual.header['Content-Type']}" would be "#{expected}") | |
end | |
failure_message_for_should_not do |actual| | |
%Q(expected that response content type "#{actual.header['Content-Type']}" would not be "#{expected}") | |
end | |
end | |
end | |
module JSONBodyMatchers | |
extend RSpec::Matchers::DSL | |
def differ | |
@differ ||= if RSpec::Expectations.const_defined?(:DiffPresenter) | |
RSpec::Expectations::DiffPresenter | |
else | |
RSpec::Expectations::Differ | |
end | |
end | |
matcher :be_json_for do |expected| | |
expected_json = expected.to_json | |
expected = JSON.parse(expected_json) | |
match do |actual| | |
expect(actual).to be_content_type 'application/json' | |
result = JSON.parse(actual.body) | |
expect(result).to eq(expected) | |
end | |
failure_message_for_should do |actual| | |
message = %Q(expected that response body [#{actual.body}]) | |
message << %Q(\nwould be [#{expected_json}]) | |
message << "\nDiff:" | |
# message << differ.new.diff_as_string(actual.body, expected_json) | |
message << differ.new.diff_as_object(JSON.parse(actual.body), expected) | |
message | |
end | |
failure_message_for_should_not do |actual| | |
message = %Q(expected that response body [#{actual.body}]) | |
message << %Q(\nwould not be [#{expected_json}].) | |
end | |
end | |
end | |
RSpec.configure do |config| | |
config.include ContentTypeMatchers | |
config.include JSONBodyMatchers | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment