Skip to content

Commit 364b8ab

Browse files
committed
Differentiate exception behavior in Rails 4.0 vs. others
NoMethodError is current_user is nil, so nil.admin? NameError is a superclass of NoMethodError (which Rails 4.0 won't allow) and means current_user might not be defined
1 parent 7d9a4bc commit 364b8ab

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

test/action_controller/serialization_scope_name_test.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ def test_serialization_scope_admin
158158
assert_equal expected_json, @response.body
159159
end
160160
end
161+
# FIXME: Has bugs. See comments below and
162+
# https://github.com/rails-api/active_model_serializers/issues/1509
161163
class NilSerializationScopeTest < ActionController::TestCase
162164
class PostViewContextTestController < ActionController::Base
163165
serialization_scope nil
@@ -195,10 +197,17 @@ def test_nil_serialization_scope_object
195197
# TODO: change to NoMethodError and match 'admin?' when the
196198
# global state in Serializer._serializer_instance_methods is fixed
197199
def test_nil_scope
198-
exception = assert_raises(NameError) do
200+
if Rails.version.start_with?('4.0')
201+
exception_class = NoMethodError
202+
exception_matcher = 'admin?'
203+
else
204+
exception_class = NameError
205+
exception_matcher = /admin|current_user/
206+
end
207+
exception = assert_raises(exception_class) do
199208
get :render_post_with_no_scope
200209
end
201-
assert_match(/admin|current_user/, exception.message)
210+
assert_match exception_matcher, exception.message
202211
end
203212

204213
# TODO: run test when

0 commit comments

Comments
 (0)