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