diff --git a/test/action_controller/serialization_test.rb b/test/action_controller/serialization_test.rb index 82da8a67..d7e3555e 100644 --- a/test/action_controller/serialization_test.rb +++ b/test/action_controller/serialization_test.rb @@ -18,6 +18,26 @@ module ActionController end end + def render_array_using_custom_root + with_adapter ActiveModel::Serializer::Adapter::Json do + @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) + render json: [@profile], root: "custom_root" + end + end + + def render_array_that_is_empty_using_custom_root + with_adapter ActiveModel::Serializer::Adapter::Json do + render json: [], root: "custom_root" + end + end + + def render_object_using_custom_root + with_adapter ActiveModel::Serializer::Adapter::Json do + @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) + render json: @profile, root: "custom_root" + end + end + def render_array_using_implicit_serializer array = [ Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), @@ -169,6 +189,30 @@ module ActionController assert_equal expected.to_json, @response.body end + def test_render_array_using_custom_root + get :render_array_using_custom_root + + expected = {custom_roots: [{name: "Name 1", description: "Description 1"}]} + assert_equal 'application/json', @response.content_type + assert_equal expected.to_json, @response.body + end + + def test_render_array_that_is_empty_using_custom_root + get :render_array_that_is_empty_using_custom_root + + expected = {custom_roots: []} + assert_equal 'application/json', @response.content_type + assert_equal expected.to_json, @response.body + end + + def test_render_object_using_custom_root + get :render_object_using_custom_root + + expected = {custom_root: {name: "Name 1", description: "Description 1"}} + assert_equal 'application/json', @response.content_type + assert_equal expected.to_json, @response.body + end + def test_render_json_object_without_serializer get :render_json_object_without_serializer