mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Bring back assert_serializer for controller testing
The `assert_serializer` test helper was added in 0.9.0.apha1[1],
and was not included in 0.10.
This patch brings back the `assert_serializer` test helper. This is the last
revision[2] that has the helper. The original helper was used as base.
[1]: https://github.com/rails-api/active_model_serializers/pull/596
[2]: 610aeb2e92
- Create the AssertSerializer
- Use the Test namespace
- Make the tests pass on the Rails master
- Rails 5 does not include `assert_template` but we need this on the tests of
the helper.
- This add the `rails-controller-testing` to keep support on `assert_template`.
- Only load test helpers in the test environment
This commit is contained in:
committed by
Benjamin Fleischer
parent
d448481b6b
commit
c0b99c980c
73
test/active_model_serializers/test/serializer_test.rb
Normal file
73
test/active_model_serializers/test/serializer_test.rb
Normal file
@@ -0,0 +1,73 @@
|
||||
require 'test_helper'
|
||||
require 'rails-controller-testing' if Rails::VERSION::MAJOR >= 5
|
||||
|
||||
module ActiveModelSerializers
|
||||
module Test
|
||||
class SerializerTest < ActionController::TestCase
|
||||
include ActiveModelSerializers::Test::Serializer
|
||||
|
||||
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
|
||||
|
||||
def render_template
|
||||
prepend_view_path './test/fixtures'
|
||||
render template: 'template'
|
||||
end
|
||||
end
|
||||
|
||||
tests MyController
|
||||
|
||||
def test_supports_specifying_serializers_with_a_serializer_class
|
||||
get :render_using_serializer
|
||||
assert_serializer ProfileSerializer
|
||||
end
|
||||
|
||||
def test_supports_specifying_serializers_with_a_regexp
|
||||
get :render_using_serializer
|
||||
assert_serializer(/\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 Hash
|
||||
end
|
||||
assert_match 'assert_serializer only accepts a String, Symbol, Regexp, ActiveModel::Serializer, or nil', e.message
|
||||
end
|
||||
|
||||
def test_does_not_overwrite_notification_subscriptions
|
||||
get :render_template
|
||||
assert_template 'template'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
1
test/fixtures/template.html.erb
vendored
Normal file
1
test/fixtures/template.html.erb
vendored
Normal file
@@ -0,0 +1 @@
|
||||
<p>Hello.</p>
|
||||
Reference in New Issue
Block a user