Created
August 10, 2012 19:16
-
-
Save jarhart/3317023 to your computer and use it in GitHub Desktop.
RSpec helper for testing your rails JSON REST API
This file contains hidden or 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 JsonHelpers | |
def json(params) | |
params = {format: 'json'}.merge(params) | |
[:get, :put, :post, :delete].find do |method| | |
path = params.delete(method) and send(method, path, params) | |
end | |
symbolize_keys(JSON.parse(response.body)) | |
end | |
private | |
def symbolize_keys(o) | |
case o | |
when Hash then Hash[o.map { |k, v| [k.to_sym, symbolize_keys(v)] }] | |
when Array then o.map { |e| symbolize_keys(e) } | |
else o | |
end | |
end | |
end | |
RSpec.configure { |config| config.include JsonHelpers, :type => :request } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment