Skip to content

Commit 1a79b11

Browse files
committed
Support Rails' parsed_body in Integration tests
Ref: https://api.rubyonrails.org/classes/ActionDispatch/TestResponse.html Close jsonapi-rb#140
1 parent 971b303 commit 1a79b11

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/jsonapi/rails/railtie.rb

Lines changed: 7 additions & 0 deletions
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'
@@ -46,6 +47,12 @@ def register_parameter_parser
4647
end
4748
end
4849

50+
def register_encoder
51+
ActiveSupport.on_load(:action_dispatch_integration_test) do
52+
register_encoder :jsonapi, response_parser: -> body { JSON.parse(body) }
53+
end
54+
end
55+
4956
# rubocop:disable Metrics/MethodLength
5057
def register_renderers
5158
ActiveSupport.on_load(:action_controller) do

spec/parsed_body_spec.rb

Lines changed: 17 additions & 0 deletions
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)