Skip to content

Instantly share code, notes, and snippets.

@valdemarua
Last active April 6, 2024 14:12
Show Gist options
  • Save valdemarua/0bd0f14d7afbd82a7a2e276945602334 to your computer and use it in GitHub Desktop.
Save valdemarua/0bd0f14d7afbd82a7a2e276945602334 to your computer and use it in GitHub Desktop.
RSpec request helper
# /spec/support/request_helpers.rb
#
# Include helper inside rails_helper.rb file:
#
# config.include RequestHelpers, type: :request
#
module RequestHelpers
#
# response body: {"data": { "id": 1 }}
#
# Returns Hash with indifferent access
# Example of usage: json[:data][:id]
# or: json["data"]["id"]
def json
parsed_json = JSON.parse(response.body)
if parsed_json.is_a?(Array)
parsed_json.map(&:with_indifferent_access)
else
parsed_json.with_indifferent_access
end
end
# Returns OpenStruct
# Example of usage: json.data.id
def jsonos
JSON.parse(response.body, object_class: OpenStruct)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment