Skip to content

Commit

Permalink
Create the AssertSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
maurogeorge committed Nov 27, 2015
1 parent 0395fad commit c99bbb7
Showing 1 changed file with 73 additions and 55 deletions.
128 changes: 73 additions & 55 deletions lib/active_model_serializers/test/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
def matches_class?
serializers.include?(expectation.name)
end

def matches_regexp?(expectation)
serializers.any? do |serializer|
serializer.match(expectation)
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
end

def matches_nil?
serializers.blank?
def a_serializer?
->(exp) { exp.is_a?(Class) && exp < ActiveModel::Serializer }
end

private

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
Expand Down

0 comments on commit c99bbb7

Please sign in to comment.