Skip to content

Commit f193a35

Browse files
bf4lgebhardt
andauthored
feat: teach JR in tests to parse the response (#1437)
* feat: teach JR in tests to parse the response * use response.parsed_body instead of JSON.parse when evaluating responses in tests --------- Co-authored-by: lgebhardt <larry@cerebris.com>
1 parent 76ef777 commit f193a35

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

lib/jsonapi/resources/railtie.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ class Railtie < ::Rails::Railtie
66
rake_tasks do
77
load 'tasks/check_upgrade.rake'
88
end
9+
10+
11+
initializer "jsonapi_resources.testing", after: :initialize do
12+
next unless Rails.env.test?
13+
# Make response.parsed_body work
14+
ActionDispatch::IntegrationTest.register_encoder :api_json,
15+
param_encoder: ->(params) {
16+
params
17+
},
18+
response_parser: ->(body) {
19+
JSONAPI::MimeTypes.parser.call(body)
20+
}
21+
end
922
end
1023
end
1124
end

test/helpers/functional_helpers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module FunctionalHelpers
5353
# end
5454
#
5555
def json_response
56-
JSON.parse(@response.body)
56+
@response.parsed_body
5757
end
5858
end
59-
end
59+
end

test/integration/requests/request_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,21 @@ def test_post_sessions
7373
'Accept' => JSONAPI::MEDIA_TYPE
7474
}
7575
assert_jsonapi_response 201
76-
json_body = JSON.parse(response.body)
76+
json_body = response.parsed_body
7777
session_id = json_body["data"]["id"]
7878

7979
# Get what we just created
8080
get "/sessions/#{session_id}?include=responses"
8181
assert_jsonapi_response 200
82-
json_body = JSON.parse(response.body)
82+
json_body = response.parsed_body
8383

8484
assert(json_body.is_a?(Object));
8585
assert(json_body["included"].is_a?(Array));
8686
assert_equal("single_textbox", json_body["included"][0]["attributes"]["response_type"]["single_textbox"]);
8787

8888
get "/sessions/#{session_id}?include=responses,responses.paragraph"
8989
assert_jsonapi_response 200
90-
json_body = JSON.parse(response.body)
90+
json_body = response.parsed_body
9191

9292
assert_equal("single_textbox", json_body["included"][0]["attributes"]["response_type"]["single_textbox"]);
9393

@@ -348,7 +348,7 @@ def test_post_polymorphic_with_has_many_relationship
348348

349349
assert_jsonapi_response 201
350350

351-
body = JSON.parse(response.body)
351+
body = response.parsed_body
352352
person = Person.find(body.dig("data", "id"))
353353

354354
assert_equal "Reo", person.name
@@ -649,7 +649,7 @@ def test_patch_polymorphic_with_has_many_relationship
649649

650650
assert_jsonapi_response 200
651651

652-
body = JSON.parse(response.body)
652+
body = response.parsed_body
653653
person = Person.find(body.dig("data", "id"))
654654

655655
assert_equal "Reo", person.name

0 commit comments

Comments
 (0)