lib/deimos/test_helpers.rb in deimos-ruby-1.11.2 vs lib/deimos/test_helpers.rb in deimos-ruby-1.12.0

- old
+ new

@@ -48,11 +48,10 @@ end included do RSpec.configure do |config| - config.prepend_before(:each) do client = double('client').as_null_object allow(client).to receive(:time) do |*_args, &block| block.call end @@ -208,13 +207,15 @@ 'offset' => 1, 'headers' => {}, 'value' => payload) unless skip_expectation - expectation = expect(handler).to receive(:consume). - with(payload, anything, &block) - expectation.and_call_original if call_original + _handler_expectation(:consume, + payload, + handler, + call_original, + &block) end Phobos::Actions::ProcessMessage.new( listener: listener, message: message, listener_metadata: { topic: 'my-topic' } @@ -275,13 +276,15 @@ 'messages' => batch_messages, 'topic' => topic_name, 'partition' => 1, 'offset_lag' => 0) unless skip_expectation - expectation = expect(handler).to receive(:consume_batch). - with(payloads, anything, &block) - expectation.and_call_original if call_original + _handler_expectation(:consume_batch, + payloads, + handler, + call_original, + &block) end action = Phobos::Actions::ProcessBatchInline.new( listener: listener, batch: batch, metadata: { topic: topic_name } @@ -353,8 +356,41 @@ listeners = Phobos.config['listeners'] handler = listeners.find { |l| l.topic == topic } raise "No consumer found in Phobos configuration for topic #{topic}!" if handler.nil? handler.handler.constantize + end + + # Test that a given handler will execute a `method` on an `input` correctly, + # If a block is given, that block will be executed when `method` is called. + # Otherwise it will just confirm that `method` is called at all. + # @param method [Symbol] + # @param input [Object] + # @param handler [Deimos::Consumer] + # @param call_original [Boolean] + def _handler_expectation(method, + input, + handler, + call_original, + &block) + schema_class = handler.class.config[:schema] + expected = input.dup + + config = handler.class.config + use_schema_classes = config[:use_schema_classes] + use_schema_classes = use_schema_classes.present? ? use_schema_classes : Deimos.config.schema.use_schema_classes + + if use_schema_classes && schema_class.present? + expected = if input.is_a?(Array) + input.map do |payload| + Utils::SchemaClass.instance(payload, schema_class) + end + else + Utils::SchemaClass.instance(input, schema_class) + end + end + + expectation = expect(handler).to receive(method).with(expected, anything, &block) + expectation.and_call_original if call_original end end end