Class: Rimless::RSpec::Matchers::HaveSentKafkaMessage

Inherits:
RSpec::Matchers::BuiltIn::BaseMatcher
  • Object
show all
Includes:
RSpec::Mocks::ExampleMethods
Defined in:
lib/rimless/rspec/matchers.rb

Overview

The Apache Kafka message expectation.

rubocop:disable Metrics/ClassLength – because its almost RSpec API

code

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ HaveSentKafkaMessage

Instantiate a new expectation object.

Parameters:

  • schema (String, Symbol, nil)

    the expected message schema



19
20
21
22
23
24
25
26
# File 'lib/rimless/rspec/matchers.rb', line 19

def initialize(schema)
  super
  @schema = schema
  @args = {}
  @data = {}
  @messages = []
  set_expected_number(:exactly, 1)
end

Instance Method Details

#at_least(count) ⇒ HaveSentKafkaMessage

Set the expected amount of message (at least).

Parameters:

  • count (Integer)

    the expected amount

Returns:



70
71
72
73
# File 'lib/rimless/rspec/matchers.rb', line 70

def at_least(count)
  set_expected_number(:at_least, count)
  self
end

#at_most(count) ⇒ HaveSentKafkaMessage

Set the expected amount of message (at most).

Parameters:

  • count (Integer)

    the expected amount

Returns:



79
80
81
82
# File 'lib/rimless/rspec/matchers.rb', line 79

def at_most(count)
  set_expected_number(:at_most, count)
  self
end

#capture { ... } ⇒ Array<Hash{Symbol => Mixed}>

Capture all Apache Kafka messages of the given block.

Yields:

  • the given block to capture the messages

Returns:

  • (Array<Hash{Symbol => Mixed}>)

    the captured messages



32
33
34
35
# File 'lib/rimless/rspec/matchers.rb', line 32

def capture(&block)
  matches?(block)
  @messages
end

#does_not_match?(proc) ⇒ Boolean

The actual RSpec API check for the expectation (negative).

rubocop:disable Naming/PredicatePrefix – because we just serve

the RSpec API here

Parameters:

  • proc (Proc)

    the block to evaluate

Returns:

  • (Boolean)

    whenever the check was unsuccessful or not



142
143
144
145
146
# File 'lib/rimless/rspec/matchers.rb', line 142

def does_not_match?(proc)
  set_expected_number(:at_least, 1)

  !matches?(proc)
end

#exactly(count) ⇒ HaveSentKafkaMessage

Set the expected amount of message (exactly).

Parameters:

  • count (Integer)

    the expected amount

Returns:



61
62
63
64
# File 'lib/rimless/rspec/matchers.rb', line 61

def exactly(count)
  set_expected_number(:exactly, count)
  self
end

#matches?(proc) ⇒ Boolean

The actual RSpec API check for the expectation.

Parameters:

  • proc (Proc)

    the block to evaluate

Returns:

  • (Boolean)

    whenever the check was successful or not



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rimless/rspec/matchers.rb', line 123

def matches?(proc)
  unless proc.is_a? Proc
    raise ArgumentError, 'have_sent_kafka_message and ' \
                         'sent_kafka_message only support block ' \
                         'expectations'
  end

  listen_to_messages
  proc.call
  check
end

#onceHaveSentKafkaMessage

Just syntactic sugar for a regular exactly(:once) call.

Returns:



94
95
96
# File 'lib/rimless/rspec/matchers.rb', line 94

def once
  exactly(:once)
end

#supports_block_expectations?Boolean

Serve the RSpec matcher API and signalize we support block evaluation.

Returns:

  • (Boolean)

    the answer



115
116
117
# File 'lib/rimless/rspec/matchers.rb', line 115

def supports_block_expectations?
  true
end

#thriceHaveSentKafkaMessage

Just syntactic sugar for a regular exactly(:thrice) call.

Returns:



108
109
110
# File 'lib/rimless/rspec/matchers.rb', line 108

def thrice
  exactly(:thrice)
end

#timesHaveSentKafkaMessage

Just syntactic sugar.

Returns:



87
88
89
# File 'lib/rimless/rspec/matchers.rb', line 87

def times
  self
end

#twiceHaveSentKafkaMessage

Just syntactic sugar for a regular exactly(:twice) call.

Returns:



101
102
103
# File 'lib/rimless/rspec/matchers.rb', line 101

def twice
  exactly(:twice)
end

#with(**args) ⇒ HaveSentKafkaMessage

Collect the expectation arguments for the Kafka message passing. (eg. topic)

Parameters:

  • args (Hash{Symbol => Mixed})

    the expected arguments

Returns:



42
43
44
45
# File 'lib/rimless/rspec/matchers.rb', line 42

def with(**args)
  @args = args
  self
end

#with_data(**args) ⇒ HaveSentKafkaMessage

Collect the expectations for the encoded message. The passed message will be decoded accordingly for the check.

Parameters:

  • args (Hash{Symbol => Mixed})

    the expected arguments

Returns:



52
53
54
55
# File 'lib/rimless/rspec/matchers.rb', line 52

def with_data(**args)
  @data = args
  self
end