/usr/lib/ruby/vendor_ruby/action_controller/serialization_test_case.rb is in ruby-active-model-serializers 0.9.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | module ActionController
module SerializationAssertions
extend ActiveSupport::Concern
included do
setup :setup_serialization_subscriptions
teardown :teardown_serialization_subscriptions
end
def setup_serialization_subscriptions
@serializers = Hash.new(0)
ActiveSupport::Notifications.subscribe("!serialize.active_model_serializers") do |name, start, finish, id, payload|
serializer = payload[:serializer]
@serializers[serializer] += 1
end
end
def teardown_serialization_subscriptions
ActiveSupport::Notifications.unsubscribe("!serialize.active_model_serializers")
end
def process(*args)
@serializers = Hash.new(0)
super
end
# Asserts that the request was rendered with the appropriate serializers.
#
# # assert that the "PostSerializer" serializer was rendered
# assert_serializer "PostSerializer"
#
# # assert that the instance of PostSerializer was rendered
# assert_serializer PostSerializer
#
# # assert that the "PostSerializer" serializer was rendered
# assert_serializer :post_serializer
#
# # assert that the rendered serializer starts with "Post"
# assert_serializer %r{\APost.+\Z}
#
# # assert that no serializer was rendered
# assert_serializer nil
#
#
def assert_serializer(options = {}, message = nil)
# Force body to be read in case the template is being streamed.
response.body
rendered = @serializers
msg = message || "expecting <#{options.inspect}> but rendering with <#{rendered.keys}>"
matches_serializer = case options
when lambda { |options| options.kind_of?(Class) && options < ActiveModel::Serializer }
rendered.any? do |serializer, count|
options.name == serializer
end
when Symbol
options = options.to_s.camelize
rendered.any? do |serializer, count|
serializer == options
end
when String
!options.empty? && rendered.any? do |serializer, count|
serializer == options
end
when Regexp
rendered.any? do |serializer, count|
serializer.match(options)
end
when NilClass
rendered.blank?
else
raise ArgumentError, "assert_serializer only accepts a String, Symbol, Regexp, ActiveModel::Serializer, or nil"
end
assert matches_serializer, msg
end
end
end
|