Skip to content

Commit 4bba16b

Browse files
committed
Factor with_adapter + force cache clear before each test.
1 parent 64168cb commit 4bba16b

File tree

4 files changed

+52
-43
lines changed

4 files changed

+52
-43
lines changed

test/action_controller/serialization_test.rb

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
require 'test_helper'
32

43
module ActionController
@@ -12,31 +11,22 @@ def render_using_implicit_serializer
1211
end
1312

1413
def render_using_default_adapter_root
15-
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
16-
# JSON-API adapter sets root by default
17-
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
18-
render json: @profile
19-
end
14+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
15+
render json: @profile
2016
end
2117

2218
def render_array_using_custom_root
23-
with_adapter ActiveModel::Serializer::Adapter::Json do
24-
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
25-
render json: [@profile], root: "custom_root"
26-
end
19+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
20+
render json: [@profile], root: "custom_root"
2721
end
2822

2923
def render_array_that_is_empty_using_custom_root
30-
with_adapter ActiveModel::Serializer::Adapter::Json do
31-
render json: [], root: "custom_root"
32-
end
24+
render json: [], root: "custom_root"
3325
end
3426

3527
def render_object_using_custom_root
36-
with_adapter ActiveModel::Serializer::Adapter::Json do
37-
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
38-
render json: @profile, root: "custom_root"
39-
end
28+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
29+
render json: @profile, root: "custom_root"
4030
end
4131

4232
def render_array_using_implicit_serializer
@@ -48,14 +38,11 @@ def render_array_using_implicit_serializer
4838
end
4939

5040
def render_array_using_implicit_serializer_and_meta
51-
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
52-
53-
@profiles = [
54-
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
55-
]
41+
@profiles = [
42+
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
43+
]
5644

57-
render json: @profiles, meta: { total: 10 }
58-
end
45+
render json: @profiles, meta: { total: 10 }
5946
end
6047

6148
def render_object_with_cache_enabled
@@ -169,8 +156,9 @@ def test_render_using_implicit_serializer
169156
end
170157

171158
def test_render_using_default_root
172-
get :render_using_default_adapter_root
173-
159+
with_adapter :json_api do
160+
get :render_using_default_adapter_root
161+
end
174162
expected = {
175163
data: {
176164
id: assigns(:profile).id.to_s,
@@ -187,23 +175,28 @@ def test_render_using_default_root
187175
end
188176

189177
def test_render_array_using_custom_root
190-
get :render_array_using_custom_root
191-
178+
with_adapter :json do
179+
get :render_array_using_custom_root
180+
end
192181
expected = {custom_roots: [{name: "Name 1", description: "Description 1"}]}
193182
assert_equal 'application/json', @response.content_type
194183
assert_equal expected.to_json, @response.body
195184
end
196185

197186
def test_render_array_that_is_empty_using_custom_root
198-
get :render_array_that_is_empty_using_custom_root
187+
with_adapter :json do
188+
get :render_array_that_is_empty_using_custom_root
189+
end
199190

200191
expected = {custom_roots: []}
201192
assert_equal 'application/json', @response.content_type
202193
assert_equal expected.to_json, @response.body
203194
end
204195

205196
def test_render_object_using_custom_root
206-
get :render_object_using_custom_root
197+
with_adapter :json do
198+
get :render_object_using_custom_root
199+
end
207200

208201
expected = {custom_root: {name: "Name 1", description: "Description 1"}}
209202
assert_equal 'application/json', @response.content_type
@@ -245,8 +238,9 @@ def test_render_array_using_implicit_serializer
245238
end
246239

247240
def test_render_array_using_implicit_serializer_and_meta
248-
get :render_array_using_implicit_serializer_and_meta
249-
241+
with_adapter :json_api do
242+
get :render_array_using_implicit_serializer_and_meta
243+
end
250244
expected = {
251245
data: [
252246
{

test/adapter/json_api/resource_type_config_test.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ def with_jsonapi_resource_type type
3232
ActiveModel::Serializer.config[:jsonapi_resource_type] = type
3333
yield
3434
ensure
35-
ActiveModel::Serializer.config[:jsonapi_resource_type] = old_type
35+
ActiveModel::Serializer.config.jsonapi_resource_type = old_type
3636
end
3737

3838
def test_config_plural
39-
with_jsonapi_resource_type :plural do
40-
serializer = CommentSerializer.new(@comment)
41-
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
42-
ActionController::Base.cache_store.clear
43-
assert_equal('comments', adapter.serializable_hash[:data][:type])
39+
with_adapter :json_api do
40+
with_jsonapi_resource_type :plural do
41+
hash = ActiveModel::SerializableResource.serialize(@comment).serializable_hash
42+
assert_equal('comments', hash[:data][:type])
43+
end
4444
end
4545
end
4646

4747
def test_config_singular
48-
with_jsonapi_resource_type :singular do
49-
serializer = CommentSerializer.new(@comment)
50-
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
51-
ActionController::Base.cache_store.clear
52-
assert_equal('comment', adapter.serializable_hash[:data][:type])
48+
with_adapter :json_api do
49+
with_jsonapi_resource_type :singular do
50+
hash = ActiveModel::SerializableResource.serialize(@comment).serializable_hash
51+
assert_equal('comment', hash[:data][:type])
52+
end
5353
end
5454
end
5555
end

test/support/serialization_testing.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Minitest::Test
2+
def before_setup
3+
ActionController::Base.cache_store.clear
4+
end
5+
6+
def with_adapter(adapter)
7+
old_adapter = ActiveModel::Serializer.config.adapter
8+
ActiveModel::Serializer.config.adapter = adapter
9+
yield
10+
ensure
11+
ActiveModel::Serializer.config.adapter = old_adapter
12+
end
13+
end

test/test_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@
3737
require 'fixtures/poro'
3838

3939
require 'support/test_case'
40+
41+
require 'support/serialization_testing'

0 commit comments

Comments
 (0)