diff --git a/lib/jsonapi/resources/railtie.rb b/lib/jsonapi/resources/railtie.rb index 8e72eb01..43d9f5c3 100644 --- a/lib/jsonapi/resources/railtie.rb +++ b/lib/jsonapi/resources/railtie.rb @@ -6,6 +6,19 @@ class Railtie < ::Rails::Railtie rake_tasks do load 'tasks/check_upgrade.rake' end + + + initializer "jsonapi_resources.testing", after: :initialize do + next unless Rails.env.test? + # Make response.parsed_body work + ActionDispatch::IntegrationTest.register_encoder :api_json, + param_encoder: ->(params) { + params + }, + response_parser: ->(body) { + JSONAPI::MimeTypes.parser.call(body) + } + end end end end diff --git a/test/helpers/functional_helpers.rb b/test/helpers/functional_helpers.rb index 3d6dc9d3..b3ce85f8 100644 --- a/test/helpers/functional_helpers.rb +++ b/test/helpers/functional_helpers.rb @@ -53,7 +53,7 @@ module FunctionalHelpers # end # def json_response - JSON.parse(@response.body) + @response.parsed_body end end -end \ No newline at end of file +end diff --git a/test/integration/requests/request_test.rb b/test/integration/requests/request_test.rb index defe3619..52531514 100644 --- a/test/integration/requests/request_test.rb +++ b/test/integration/requests/request_test.rb @@ -73,13 +73,13 @@ def test_post_sessions 'Accept' => JSONAPI::MEDIA_TYPE } assert_jsonapi_response 201 - json_body = JSON.parse(response.body) + json_body = response.parsed_body session_id = json_body["data"]["id"] # Get what we just created get "/sessions/#{session_id}?include=responses" assert_jsonapi_response 200 - json_body = JSON.parse(response.body) + json_body = response.parsed_body assert(json_body.is_a?(Object)); assert(json_body["included"].is_a?(Array)); @@ -87,7 +87,7 @@ def test_post_sessions get "/sessions/#{session_id}?include=responses,responses.paragraph" assert_jsonapi_response 200 - json_body = JSON.parse(response.body) + json_body = response.parsed_body assert_equal("single_textbox", json_body["included"][0]["attributes"]["response_type"]["single_textbox"]); @@ -348,7 +348,7 @@ def test_post_polymorphic_with_has_many_relationship assert_jsonapi_response 201 - body = JSON.parse(response.body) + body = response.parsed_body person = Person.find(body.dig("data", "id")) assert_equal "Reo", person.name @@ -649,7 +649,7 @@ def test_patch_polymorphic_with_has_many_relationship assert_jsonapi_response 200 - body = JSON.parse(response.body) + body = response.parsed_body person = Person.find(body.dig("data", "id")) assert_equal "Reo", person.name