Skip to content

Instantly share code, notes, and snippets.

@chebyte
Created June 28, 2013 15:51
Show Gist options
  • Save chebyte/5885717 to your computer and use it in GitHub Desktop.
Save chebyte/5885717 to your computer and use it in GitHub Desktop.
custom rspec matcher for json
#spec/supports/custom_matchers.rb
RSpec::Matchers.define :be_json do |expected|
match do |actual|
actual = ActiveSupport::JSON.decode(actual).with_indifferent_access
expected = ActiveSupport::JSON.decode(expected) unless expected.is_a?(Hash)
expected = expected.with_indifferent_access
actual.diff(expected) == {}
end
end
#spec
expect(response.body).to be_json({"error"=>"Unauthorized"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment