mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Restore test-local subscriber teardown
This commit revises 0ce4ad35a12ffd858f6a5d7bbeb48fc1e2cfac92
`Remove unused/unusable unsubscribe since we don't want to unsubscribe`
Looking at Rails implementation of assert_template
which was likely the inspiration for assert_serializer:
f756b33c13/lib/rails/controller/testing/template_assertions.rb
Ref:
- https://github.com/rails-api/active_model_serializers/pull/596
- https://github.com/rails-api/active_model_serializers/pull/620
- https://github.com/rails-api/active_model_serializers/issues/616
This commit is contained in:
parent
e60937bc2f
commit
9779185d57
@ -6,6 +6,7 @@ module ActiveModelSerializers
|
||||
|
||||
included do
|
||||
setup :setup_serialization_subscriptions
|
||||
teardown :teardown_serialization_subscriptions
|
||||
end
|
||||
|
||||
# Asserts that the request was rendered with the appropriate serializers.
|
||||
@ -41,6 +42,7 @@ module ActiveModelSerializers
|
||||
|
||||
def initialize
|
||||
@serializers = Set.new
|
||||
@_subscribers = []
|
||||
end
|
||||
|
||||
def message=(message)
|
||||
@ -62,12 +64,18 @@ module ActiveModelSerializers
|
||||
end
|
||||
|
||||
def subscribe
|
||||
ActiveSupport::Notifications.subscribe(event_name) do |_name, _start, _finish, _id, payload|
|
||||
@_subscribers << ActiveSupport::Notifications.subscribe(event_name) do |_name, _start, _finish, _id, payload|
|
||||
serializer = payload[:serializer].name
|
||||
serializers << serializer
|
||||
end
|
||||
end
|
||||
|
||||
def unsubscribe
|
||||
@_subscribers.each do |subscriber|
|
||||
ActiveSupport::Notifications.unsubscribe(subscriber)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def matches_class?
|
||||
@ -108,6 +116,10 @@ module ActiveModelSerializers
|
||||
@assert_serializer = AssertSerializer.new
|
||||
@assert_serializer.subscribe
|
||||
end
|
||||
|
||||
def teardown_serialization_subscriptions
|
||||
@assert_serializer.unsubscribe
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user