Skip to content

Commit cd6cca3

Browse files
taglialaremear
andauthored
Support Rails' parsed_body in Integration tests (jsonapi-rb#143)
* Support Rails' `parsed_body` in Integration tests Ref: https://api.rubyonrails.org/classes/ActionDispatch/TestResponse.html Close jsonapi-rb#140 * Add param_encoder to register_encoder --------- Co-authored-by: Ben Mills <[email protected]>
1 parent 9af48a5 commit cd6cca3

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Diff for: lib/jsonapi/rails/railtie.rb

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Railtie < ::Rails::Railtie
2323
initializer 'jsonapi-rails.init' do |app|
2424
register_mime_type
2525
register_parameter_parser
26+
register_encoder
2627
register_renderers
2728
ActiveSupport.on_load(:action_controller) do
2829
require 'jsonapi/rails/controller'
@@ -42,6 +43,14 @@ def register_parameter_parser
4243
ActionDispatch::Request.parameter_parsers[:jsonapi] = PARSER
4344
end
4445

46+
def register_encoder
47+
ActiveSupport.on_load(:action_dispatch_integration_test) do
48+
register_encoder :jsonapi,
49+
param_encoder: -> (params) { params.to_json },
50+
response_parser: -> (body) { JSON.parse(body) }
51+
end
52+
end
53+
4554
# rubocop:disable Metrics/MethodLength
4655
def register_renderers
4756
ActiveSupport.on_load(:action_controller) do

Diff for: spec/parsed_body_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'rails_helper'
2+
3+
describe ActionController::Base, type: :controller, if: Rails.version >= '6.0' do
4+
controller do
5+
def index
6+
render jsonapi: nil
7+
end
8+
end
9+
10+
subject(:parsed_body) { response.parsed_body }
11+
12+
it 'allows using parsed_body in ActionController::TestCase' do
13+
get :index
14+
15+
expect(parsed_body).to eq(JSON.parse(response.body))
16+
end
17+
end

0 commit comments

Comments
 (0)