Factor with_adapter + force cache clear before each test.

This commit is contained in:
Lucas Hosseini 2015-08-28 17:00:24 +02:00
parent 64168cbecd
commit 4bba16bf4e
4 changed files with 52 additions and 43 deletions

View File

@ -1,4 +1,3 @@
require 'test_helper'
module ActionController
@ -12,31 +11,22 @@ module ActionController
end
def render_using_default_adapter_root
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
# JSON-API adapter sets root by default
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile
end
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile
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
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: [@profile], root: "custom_root"
end
def render_array_that_is_empty_using_custom_root
with_adapter ActiveModel::Serializer::Adapter::Json do
render json: [], root: "custom_root"
end
render json: [], root: "custom_root"
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
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
render json: @profile, root: "custom_root"
end
def render_array_using_implicit_serializer
@ -48,14 +38,11 @@ module ActionController
end
def render_array_using_implicit_serializer_and_meta
with_adapter ActiveModel::Serializer::Adapter::JsonApi do
@profiles = [
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
]
@profiles = [
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
]
render json: @profiles, meta: { total: 10 }
end
render json: @profiles, meta: { total: 10 }
end
def render_object_with_cache_enabled
@ -169,8 +156,9 @@ module ActionController
end
def test_render_using_default_root
get :render_using_default_adapter_root
with_adapter :json_api do
get :render_using_default_adapter_root
end
expected = {
data: {
id: assigns(:profile).id.to_s,
@ -187,15 +175,18 @@ module ActionController
end
def test_render_array_using_custom_root
get :render_array_using_custom_root
with_adapter :json do
get :render_array_using_custom_root
end
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
with_adapter :json do
get :render_array_that_is_empty_using_custom_root
end
expected = {custom_roots: []}
assert_equal 'application/json', @response.content_type
@ -203,7 +194,9 @@ module ActionController
end
def test_render_object_using_custom_root
get :render_object_using_custom_root
with_adapter :json do
get :render_object_using_custom_root
end
expected = {custom_root: {name: "Name 1", description: "Description 1"}}
assert_equal 'application/json', @response.content_type
@ -245,8 +238,9 @@ module ActionController
end
def test_render_array_using_implicit_serializer_and_meta
get :render_array_using_implicit_serializer_and_meta
with_adapter :json_api do
get :render_array_using_implicit_serializer_and_meta
end
expected = {
data: [
{

View File

@ -32,24 +32,24 @@ module ActiveModel
ActiveModel::Serializer.config[:jsonapi_resource_type] = type
yield
ensure
ActiveModel::Serializer.config[:jsonapi_resource_type] = old_type
ActiveModel::Serializer.config.jsonapi_resource_type = old_type
end
def test_config_plural
with_jsonapi_resource_type :plural do
serializer = CommentSerializer.new(@comment)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
ActionController::Base.cache_store.clear
assert_equal('comments', adapter.serializable_hash[:data][:type])
with_adapter :json_api do
with_jsonapi_resource_type :plural do
hash = ActiveModel::SerializableResource.serialize(@comment).serializable_hash
assert_equal('comments', hash[:data][:type])
end
end
end
def test_config_singular
with_jsonapi_resource_type :singular do
serializer = CommentSerializer.new(@comment)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
ActionController::Base.cache_store.clear
assert_equal('comment', adapter.serializable_hash[:data][:type])
with_adapter :json_api do
with_jsonapi_resource_type :singular do
hash = ActiveModel::SerializableResource.serialize(@comment).serializable_hash
assert_equal('comment', hash[:data][:type])
end
end
end
end

View File

@ -0,0 +1,13 @@
class Minitest::Test
def before_setup
ActionController::Base.cache_store.clear
end
def with_adapter(adapter)
old_adapter = ActiveModel::Serializer.config.adapter
ActiveModel::Serializer.config.adapter = adapter
yield
ensure
ActiveModel::Serializer.config.adapter = old_adapter
end
end

View File

@ -37,3 +37,5 @@ require 'support/rails_app'
require 'fixtures/poro'
require 'support/test_case'
require 'support/serialization_testing'