|
3 | 3 | module ActionController
|
4 | 4 | module Serialization
|
5 | 5 | class ExplicitSerializerTest < ActionController::TestCase
|
6 |
| - class MyController < ActionController::Base |
| 6 | + class ExplicitSerializerTestController < ActionController::Base |
7 | 7 | def render_using_explicit_serializer
|
8 | 8 | @profile = Profile.new(name: 'Name 1',
|
9 | 9 | description: 'Description 1',
|
@@ -55,9 +55,16 @@ def render_array_using_explicit_serializer_and_custom_serializers
|
55 | 55 |
|
56 | 56 | render json: [@post], each_serializer: PostPreviewSerializer
|
57 | 57 | end
|
| 58 | + |
| 59 | + def render_using_explicit_each_serializer |
| 60 | + location = Location.new(id: 42, lat: '-23.550520', lng: '-46.633309') |
| 61 | + place = Place.new(id: 1337, name: 'Amazing Place', locations: [location]) |
| 62 | + |
| 63 | + render json: place, each_serializer: PlaceSerializer |
| 64 | + end |
58 | 65 | end
|
59 | 66 |
|
60 |
| - tests MyController |
| 67 | + tests ExplicitSerializerTestController |
61 | 68 |
|
62 | 69 | def test_render_using_explicit_serializer
|
63 | 70 | get :render_using_explicit_serializer
|
@@ -105,6 +112,25 @@ def test_render_array_using_explicit_serializer_and_custom_serializers
|
105 | 112 |
|
106 | 113 | assert_equal expected.to_json, @response.body
|
107 | 114 | end
|
| 115 | + |
| 116 | + def test_render_using_explicit_each_serializer |
| 117 | + get :render_using_explicit_each_serializer |
| 118 | + |
| 119 | + expected = { |
| 120 | + id: 1337, |
| 121 | + name: "Amazing Place", |
| 122 | + locations: [ |
| 123 | + { |
| 124 | + id: 42, |
| 125 | + lat: "-23.550520", |
| 126 | + lng: "-46.633309", |
| 127 | + place: "Nowhere" # is a virtual attribute on LocationSerializer |
| 128 | + } |
| 129 | + ] |
| 130 | + } |
| 131 | + |
| 132 | + assert_equal expected.to_json, response.body |
| 133 | + end |
108 | 134 | end
|
109 | 135 | end
|
110 | 136 | end
|
0 commit comments