|
| 1 | +require 'set' |
| 2 | +module ActiveModelSerializers |
| 3 | + module Test |
| 4 | + module Serializer |
| 5 | + extend ActiveSupport::Concern |
| 6 | + |
| 7 | + included do |
| 8 | + setup :setup_serialization_subscriptions |
| 9 | + teardown :teardown_serialization_subscriptions |
| 10 | + end |
| 11 | + |
| 12 | + # Asserts that the request was rendered with the appropriate serializers. |
| 13 | + # |
| 14 | + # # assert that the "PostSerializer" serializer was rendered |
| 15 | + # assert_serializer "PostSerializer" |
| 16 | + # |
| 17 | + # # return a custom error message |
| 18 | + # assert_serializer "PostSerializer", "PostSerializer not rendered" |
| 19 | + # |
| 20 | + # # assert that the instance of PostSerializer was rendered |
| 21 | + # assert_serializer PostSerializer |
| 22 | + # |
| 23 | + # # assert that the "PostSerializer" serializer was rendered |
| 24 | + # assert_serializer :post_serializer |
| 25 | + # |
| 26 | + # # assert that the rendered serializer starts with "Post" |
| 27 | + # assert_serializer %r{\APost.+\Z} |
| 28 | + # |
| 29 | + # # assert that no serializer was rendered |
| 30 | + # assert_serializer nil |
| 31 | + # |
| 32 | + def assert_serializer(expectation, message = nil) |
| 33 | + @assert_serializer.expectation = expectation |
| 34 | + @assert_serializer.message = message |
| 35 | + @assert_serializer.response = response |
| 36 | + assert(@assert_serializer.matches?, @assert_serializer.message) |
| 37 | + end |
| 38 | + |
| 39 | + class AssertSerializer |
| 40 | + attr_reader :serializers, :message |
| 41 | + attr_accessor :response, :expectation |
| 42 | + |
| 43 | + def initialize |
| 44 | + @serializers = Set.new |
| 45 | + @_subscribers = [] |
| 46 | + end |
| 47 | + |
| 48 | + def message=(message) |
| 49 | + @message = message || "expecting <#{expectation.inspect}> but rendering with <#{serializers.to_a}>" |
| 50 | + end |
| 51 | + |
| 52 | + def matches? |
| 53 | + # Force body to be read in case the template is being streamed. |
| 54 | + response.body |
| 55 | + |
| 56 | + case expectation |
| 57 | + when a_serializer? then matches_class? |
| 58 | + when Symbol then matches_symbol? |
| 59 | + when String then matches_string? |
| 60 | + when Regexp then matches_regexp? |
| 61 | + when NilClass then matches_nil? |
| 62 | + else fail ArgumentError, 'assert_serializer only accepts a String, Symbol, Regexp, ActiveModel::Serializer, or nil' |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + def subscribe |
| 67 | + @_subscribers << ActiveSupport::Notifications.subscribe(event_name) do |_name, _start, _finish, _id, payload| |
| 68 | + serializer = payload[:serializer].name |
| 69 | + serializers << serializer |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + def unsubscribe |
| 74 | + @_subscribers.each do |subscriber| |
| 75 | + ActiveSupport::Notifications.unsubscribe(subscriber) |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + private |
| 80 | + |
| 81 | + def matches_class? |
| 82 | + serializers.include?(expectation.name) |
| 83 | + end |
| 84 | + |
| 85 | + def matches_symbol? |
| 86 | + camelize_expectation = expectation.to_s.camelize |
| 87 | + serializers.include?(camelize_expectation) |
| 88 | + end |
| 89 | + |
| 90 | + def matches_string? |
| 91 | + !expectation.empty? && serializers.include?(expectation) |
| 92 | + end |
| 93 | + |
| 94 | + def matches_regexp? |
| 95 | + serializers.any? do |serializer| |
| 96 | + serializer.match(expectation) |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + def matches_nil? |
| 101 | + serializers.empty? |
| 102 | + end |
| 103 | + |
| 104 | + def a_serializer? |
| 105 | + ->(exp) { exp.is_a?(Class) && exp < ActiveModel::Serializer } |
| 106 | + end |
| 107 | + |
| 108 | + def event_name |
| 109 | + ::ActiveModelSerializers::Logging::RENDER_EVENT |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + private |
| 114 | + |
| 115 | + def setup_serialization_subscriptions |
| 116 | + @assert_serializer = AssertSerializer.new |
| 117 | + @assert_serializer.subscribe |
| 118 | + end |
| 119 | + |
| 120 | + def teardown_serialization_subscriptions |
| 121 | + @assert_serializer.unsubscribe |
| 122 | + end |
| 123 | + end |
| 124 | + end |
| 125 | +end |
0 commit comments