Module: Rimless::RSpec::Helpers
- Defined in:
- lib/rimless/rspec/helpers.rb
Overview
A collection of Rimless/RSpec helpers.
Instance Method Summary collapse
-
#avro_parse(data) ⇒ Hash{String => Mixed}
A simple helper to parse a blob of Apache Avro data.
-
#capture_kafka_messages { ... } ⇒ Array<Hash{Symbol => Mixed}>
Capture all Apache Kafka messages of the given block.
-
#kafka_consumer_for(topic) ⇒ Karafka::BaseConsumer
An augmented helper for
karafka.consumer_for, provided by thekarafka-testinggem to locate and instantiate a consumer. -
#kafka_message(topic: nil, headers: {}, metadata: {}, **payload) ⇒ RSpec::Mocks::InstanceVerifyingDouble
A simple helper to generate Apache Kafka message doubles for consuming.
Instance Method Details
#avro_parse(data) ⇒ Hash{String => Mixed}
A simple helper to parse a blob of Apache Avro data.
13 14 15 |
# File 'lib/rimless/rspec/helpers.rb', line 13 def avro_parse(data, **) Rimless.avro_decode(data, **) end |
#capture_kafka_messages { ... } ⇒ Array<Hash{Symbol => Mixed}>
Capture all Apache Kafka messages of the given block.
57 58 59 |
# File 'lib/rimless/rspec/helpers.rb', line 57 def (&) Rimless::RSpec::Matchers::HaveSentKafkaMessage.new(nil).capture(&) end |
#kafka_consumer_for(topic) ⇒ Karafka::BaseConsumer
An augmented helper for karafka.consumer_for, provided by the karafka-testing gem to locate and instantiate a consumer. When the found consumer features the Rimless job bridge consumer logic, the enqueue_job is replaced to not enqueue the job, but perform it inline. Otherwise the end-user consumer logic is not executed, which is clearly the user expectation.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rimless/rspec/helpers.rb', line 71 def kafka_consumer_for(topic) # The +karafka+ helper is provided by the +karafka-testing+ gem karafka.consumer_for(topic).tap do |consumer| # When we're not dealing with a regular Rimless job bridge consumer, # we skip further processing next unless consumer.respond_to? :enqueue_job # Otherwise rig the job bridging and run the wrapped consumer instead allow(consumer).to receive(:enqueue_job) do || Rimless.configuration.consumer_job_class.perform_now( **consumer.() ) end end end |
#kafka_message(topic: nil, headers: {}, metadata: {}, **payload) ⇒ RSpec::Mocks::InstanceVerifyingDouble
A simple helper to generate Apache Kafka message doubles for consuming.
rubocop:disable Metrics/MethodLength – because of the metadata handling
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 |
# File 'lib/rimless/rspec/helpers.rb', line 26 def (topic: nil, headers: {}, metadata: {}, **payload) = { topic: topic ? Rimless.topic(topic) : nil, partition: 0, offset: 206, key: nil, headers: headers, timestamp: Time.current, received_at: Time.current, ** } instance_double( Karafka::Messages::Message, deserialized?: true, tombstone?: false, payload: payload, metadata: instance_double( Karafka::Messages::Metadata, **, to_h: ), ** ) end |