diff --git a/lib/active_model_serializers/test/serializer.rb b/lib/active_model_serializers/test/serializer.rb index 76b6c371b..8e8c95498 100644 --- a/lib/active_model_serializers/test/serializer.rb +++ b/lib/active_model_serializers/test/serializer.rb @@ -5,7 +5,6 @@ module Serializer included do setup :setup_serialization_subscriptions - teardown :teardown_serialization_subscriptions end # Asserts that the request was rendered with the appropriate serializers. @@ -29,75 +28,94 @@ module Serializer # assert_serializer nil # def assert_serializer(expectation, message = nil) - # Force body to be read in case the template is being streamed. - response.body - - msg = message || "expecting <#{expectation.inspect}> but rendering with <#{serializers}>" - - matches_serializer = case expectation - when a_serializer? - matches_class?(expectation) - when Symbol - matches_symbol?(expectation) - when String - matches_string?(expectation) - when Regexp - matches_regexp?(expectation) - when NilClass - matches_nil? - else - fail ArgumentError, 'assert_serializer only accepts a String, Symbol, Regexp, ActiveModel::Serializer, or nil' - end - assert(matches_serializer, msg) + @assert_serializer.expectation = expectation + @assert_serializer.message = message + @assert_serializer.response = response + assert(@assert_serializer.matches?, @assert_serializer.message) end - private + class AssertSerializer + attr_reader :serializers, :message + attr_accessor :response, :expectation - ActiveModelSerializers.silence_warnings do - attr_reader :serializers, :expectation - end + def initialize + @serializers = [] + end - def setup_serialization_subscriptions - @serializers = [] - ActiveSupport::Notifications.subscribe(event_name) do |_name, _start, _finish, _id, payload| - serializer = payload[:serializer].name - serializers << serializer + def message=(message) + @message = message || "expecting <#{expectation.inspect}> but rendering with <#{serializers}>" end - end - def teardown_serialization_subscriptions - ActiveSupport::Notifications.unsubscribe(event_name) - end + def matches? + # Force body to be read in case the template is being streamed. + response.body - def event_name - 'render.active_model_serializers' - end + case expectation + when a_serializer? + matches_class? + when Symbol + matches_symbol? + when String + matches_string? + when Regexp + matches_regexp? + when NilClass + matches_nil? + else + fail ArgumentError, 'assert_serializer only accepts a String, Symbol, Regexp, ActiveModel::Serializer, or nil' + end + end - def matches_class?(expectation) - serializers.include?(expectation.name) - end + def subscribe + ActiveSupport::Notifications.subscribe(event_name) do |_name, _start, _finish, _id, payload| + serializer = payload[:serializer].name + serializers << serializer + end + end - def matches_symbol?(expectation) - expectation = expectation.to_s.camelize - serializers.include?(expectation) - end + def unsubscribe + ActiveSupport::Notifications.unsubscribe(event_name) + end - def matches_string?(expectation) - !expectation.empty? && serializers.include?(expectation) - end + private - def matches_regexp?(expectation) - serializers.any? do |serializer| - serializer.match(expectation) + def matches_class? + serializers.include?(expectation.name) end - end - def matches_nil? - serializers.blank? + def matches_symbol? + camelize_expectation = expectation.to_s.camelize + serializers.include?(camelize_expectation) + end + + def matches_string? + !expectation.empty? && serializers.include?(expectation) + end + + def matches_regexp? + serializers.any? do |serializer| + serializer.match(expectation) + end + end + + def matches_nil? + serializers.blank? + end + + def a_serializer? + ->(exp) { exp.is_a?(Class) && exp < ActiveModel::Serializer } + end + + def event_name + 'render.active_model_serializers' + end end - def a_serializer? - ->(exp) { exp.is_a?(Class) && exp < ActiveModel::Serializer } + private + + def setup_serialization_subscriptions + @assert_serializer = AssertSerializer.new + @assert_serializer.subscribe end end end