mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Test::Unit assert_serializer implemented
So you can assert specific serializer to be used.
This commit is contained in:
committed by
Tema Bolshakov
parent
be6ff586a2
commit
eaedcefa4e
55
test/integration/action_controller/test_case_test.rb
Normal file
55
test/integration/action_controller/test_case_test.rb
Normal file
@@ -0,0 +1,55 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActionController
|
||||
module SerializationsAssertions
|
||||
class RenderSerializerTest < ActionController::TestCase
|
||||
class MyController < ActionController::Base
|
||||
def render_using_serializer
|
||||
render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
end
|
||||
|
||||
def render_text
|
||||
render text: 'ok'
|
||||
end
|
||||
end
|
||||
|
||||
tests MyController
|
||||
|
||||
def test_supports_specifying_serializers_with_a_regexp
|
||||
get :render_using_serializer
|
||||
assert_serializer %r{\AProfile.+\Z}
|
||||
end
|
||||
|
||||
def test_supports_specifying_serializers_with_a_string
|
||||
get :render_using_serializer
|
||||
assert_serializer 'ProfileSerializer'
|
||||
end
|
||||
|
||||
def test_supports_specifying_serializers_with_a_symbol
|
||||
get :render_using_serializer
|
||||
assert_serializer :profile_serializer
|
||||
end
|
||||
|
||||
def test_supports_specifying_serializers_with_a_nil
|
||||
get :render_text
|
||||
assert_serializer nil
|
||||
end
|
||||
|
||||
def test_raises_descriptive_error_message_when_serializer_was_not_rendered
|
||||
get :render_using_serializer
|
||||
e = assert_raise ActiveSupport::TestCase::Assertion do
|
||||
assert_serializer 'PostSerializer'
|
||||
end
|
||||
assert_match 'expecting <"PostSerializer"> but rendering with <["ProfileSerializer"]>', e.message
|
||||
end
|
||||
|
||||
def test_raises_argument_error_when_asserting_with_invalid_object
|
||||
get :render_using_serializer
|
||||
e = assert_raise ArgumentError do
|
||||
assert_serializer OpenStruct.new
|
||||
end
|
||||
assert_match 'assert_serializer only accepts a String, Symbol, Regexp, or nil', e.message
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user