Skip to content

Commit 2edd39d

Browse files
committed
Need to teardown the dynamically added method
1 parent a065bc2 commit 2edd39d

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

test/action_controller/serialization_scope_name_test.rb

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
require 'test_helper'
22

33
module SerializationScopeTesting
4+
def self.undef_serializer_dynamic_scope_methods
5+
[PostSerializer, PostViewContextSerializer]. each do |serializer_class|
6+
instance_method_cache = serializer_class.instance_variable_get(:@_serializer_instance_methods)
7+
class_method_cache = serializer_class.class_variable_get(:@@_serializer_instance_methods)[serializer_class]
8+
[:view_context, :current_user].each do |scope_method|
9+
serializer_class.send(:undef_method, scope_method) if serializer_class.method_defined?(scope_method)
10+
instance_method_cache && instance_method_cache.delete(scope_method)
11+
class_method_cache && class_method_cache.delete(scope_method)
12+
end
13+
end
14+
end
415
class User < ActiveModelSerializers::Model
516
attr_accessor :id, :name, :admin
617
def admin?
@@ -70,6 +81,10 @@ def comments
7081
class DefaultScopeTest < ActionController::TestCase
7182
tests PostTestController
7283

84+
teardown do
85+
SerializationScopeTesting.undef_serializer_dynamic_scope_methods
86+
end
87+
7388
def test_default_serialization_scope
7489
assert_equal :current_user, @controller._serialization_scope
7590
end
@@ -120,6 +135,10 @@ def serializer
120135
end
121136
tests PostViewContextTestController
122137

138+
teardown do
139+
SerializationScopeTesting.undef_serializer_dynamic_scope_methods
140+
end
141+
123142
def test_defined_serialization_scope
124143
assert_equal :view_context, @controller._serialization_scope
125144
end
@@ -187,6 +206,10 @@ def new_post
187206
end
188207
tests PostViewContextTestController
189208

209+
teardown do
210+
SerializationScopeTesting.undef_serializer_dynamic_scope_methods
211+
end
212+
190213
def test_nil_serialization_scope
191214
assert_nil @controller._serialization_scope
192215
end
@@ -196,14 +219,8 @@ def test_nil_serialization_scope_object
196219
end
197220

198221
def test_nil_scope
199-
if Rails.version.start_with?('4.0')
200-
exception_class = NoMethodError
201-
exception_matcher = 'admin?'
202-
else
203-
exception_class = NameError
204-
exception_matcher = /admin|current_user/
205-
end
206-
exception = assert_raises(exception_class) do
222+
exception_matcher = /current_user/
223+
exception = assert_raises(NameError) do
207224
get :render_post_with_no_scope
208225
end
209226
assert_match exception_matcher, exception.message
@@ -225,18 +242,11 @@ def test_serialization_scope_is_and_nil_scope_passed_in_current_user
225242
end
226243

227244
def test_serialization_scope_is_nil_and_scope_passed_in_current_user_without_scope_name
228-
get :render_post_with_passed_in_scope_without_scope_name
229-
expected_json = {
230-
post: {
231-
id: 4,
232-
title: 'Title',
233-
body: "The 'scope' is the 'current_user': true",
234-
comments: [
235-
{ id: 2, body: 'Scoped' }
236-
]
237-
}
238-
}.to_json
239-
assert_equal expected_json, @response.body
245+
exception_matcher = /current_user/
246+
exception = assert_raises(NameError) do
247+
get :render_post_with_passed_in_scope_without_scope_name
248+
end
249+
assert_match exception_matcher, exception.message
240250
end
241251
end
242252
end

0 commit comments

Comments
 (0)