Skip to content

Commit a43cff4

Browse files
committed
Remove assert_template from Test::SerializerTest
Rails 5 removed this assertion after considering it not a good testing practice. rails/rails#18950 Rather that add a gem to our Rails 5 matrix to support it, the assertion is made that the template is rendering using active support notifications. Also, to clarify that the action 'render_template' is unrelated to the event name '!render_template.action_view', I renamed the actions so that would not look like event names.
1 parent f594314 commit a43cff4

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ version = ENV['RAILS_VERSION'] || '4.2'
1212
if version == 'master'
1313
gem 'rack', github: 'rack/rack'
1414
gem 'arel', github: 'rails/arel'
15-
gem 'rails-controller-testing', github: 'rails/rails-controller-testing'
1615
git 'https://github.com/rails/rails.git' do
1716
gem 'railties'
1817
gem 'activesupport'

test/active_model_serializers/test/serializer_test.rb

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
require 'test_helper'
2-
require 'rails-controller-testing' if Rails::VERSION::MAJOR >= 5
32

43
module ActiveModelSerializers
54
module Test
65
class SerializerTest < ActionController::TestCase
76
include ActiveModelSerializers::Test::Serializer
87

98
class MyController < ActionController::Base
9+
TEMPLATE_NAME = 'template'
1010
def render_using_serializer
1111
render json: Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
1212
end
1313

14-
def render_text
15-
render text: 'ok'
14+
# For Rails4.0
15+
def render_some_text
16+
Rails.version > '4.1' ? render(plain: 'ok') : render(text: 'ok')
1617
end
1718

18-
def render_template
19+
def render_a_template
1920
prepend_view_path './test/fixtures'
20-
render template: 'template'
21+
render template: TEMPLATE_NAME
2122
end
2223
end
2324

@@ -44,7 +45,7 @@ def test_supports_specifying_serializers_with_a_symbol
4445
end
4546

4647
def test_supports_specifying_serializers_with_a_nil
47-
get :render_text
48+
get :render_some_text
4849
assert_serializer nil
4950
end
5051

@@ -65,8 +66,20 @@ def test_raises_argument_error_when_asserting_with_invalid_object
6566
end
6667

6768
def test_does_not_overwrite_notification_subscriptions
68-
get :render_template
69-
assert_template 'template'
69+
payloads = []
70+
event_name = '!render_template.action_view'
71+
ActiveSupport::Notifications.subscribe(event_name) do |_name, _start, _finish, _id, payload|
72+
payloads << payload
73+
end
74+
75+
get :render_a_template
76+
77+
assert_equal 1, payloads.size, 'Only expected one template rendering to be registered'
78+
payload = payloads.first
79+
assert_equal MyController::TEMPLATE_NAME, payload[:virtual_path]
80+
assert_match %r{test/fixtures/#{MyController::TEMPLATE_NAME}.html.erb}, payload[:identifier]
81+
ensure
82+
ActiveSupport::Notifications.unsubscribe(event_name)
7083
end
7184
end
7285
end

0 commit comments

Comments
 (0)