Module: Lepus::Testing

Defined in:
lib/lepus/testing.rb,
lib/lepus/testing/exchange.rb,
lib/lepus/testing/rspec_matchers.rb,
lib/lepus/testing/message_builder.rb

Defined Under Namespace

Modules: RSpecMatchers Classes: Exchange, MessageBuilder

Class Method Summary collapse

Class Method Details

.clear_all_messages!Object

Clear all messages from all fake exchanges



55
56
57
# File 'lib/lepus/testing.rb', line 55

def clear_all_messages!
  Exchange.clear_all_messages!
end

.consumer_capture_errors!Object

Disable consumer error re-raising (default behavior converts to :reject)



45
46
47
# File 'lib/lepus/testing.rb', line 45

def consumer_capture_errors!
  @consumer_raise_errors_enabled = false
end

.consumer_perform(consumer_class, message_or_payload) ⇒ Symbol

Test a consumer with a message

Parameters:

  • consumer_class (Class)

    The consumer class to test

  • message_or_payload (Lepus::Message, Hash, String)

    The message to process

Returns:

  • (Symbol)

    The result of the consumer’s perform method (:ack, :reject, :requeue, :nack)



86
87
88
89
90
# File 'lib/lepus/testing.rb', line 86

def consumer_perform(consumer_class, message_or_payload)
  message = build_message(message_or_payload)
  consumer = consumer_class.new
  consumer.process_delivery(message.delivery_info, message., message.payload)
end

.consumer_raise_errors!Object

When enabled, consumer exceptions are re-raised instead of being converted to :reject This is useful in specs, so RSpec can capture failures instead of false positives



40
41
42
# File 'lib/lepus/testing.rb', line 40

def consumer_raise_errors!
  @consumer_raise_errors_enabled = true
end

.consumer_raise_errors?Boolean

Returns true if consumer errors should be re-raised in tests

Returns:

  • (Boolean)


50
51
52
# File 'lib/lepus/testing.rb', line 50

def consumer_raise_errors?
  @consumer_raise_errors_enabled == true
end

.disable!Object

Disable fake publishing mode and consumer error re-raising



17
18
19
20
# File 'lib/lepus/testing.rb', line 17

def disable!
  fake_publisher_disable!
  consumer_capture_errors!
end

.enable!Object

Enable fake publishing mode and consumer error re-raising



11
12
13
14
# File 'lib/lepus/testing.rb', line 11

def enable!
  fake_publisher!
  consumer_raise_errors!
end

.exchange(name) ⇒ Object

Get a specific fake exchange by name



65
66
67
# File 'lib/lepus/testing.rb', line 65

def exchange(name)
  Exchange[name]
end

.exchangesObject

Get all fake exchanges



60
61
62
# File 'lib/lepus/testing.rb', line 60

def exchanges
  Exchange.all
end

.fake_publisher!Object

Enable fake publishing mode for testing When enabled, messages are stored in fake exchanges instead of being sent to RabbitMQ



24
25
26
# File 'lib/lepus/testing.rb', line 24

def fake_publisher!
  @fake_publisher_enabled = true
end

.fake_publisher_disable!Object

Disable fake publishing mode for testing



34
35
36
# File 'lib/lepus/testing.rb', line 34

def fake_publisher_disable!
  @fake_publisher_enabled = false
end

.fake_publisher_enabled?Boolean

Check if fake publishing is enabled

Returns:

  • (Boolean)


29
30
31
# File 'lib/lepus/testing.rb', line 29

def fake_publisher_enabled?
  @fake_publisher_enabled == true
end

.message_builderMessageBuilder

Create a message builder for custom scenarios

Returns:



94
95
96
# File 'lib/lepus/testing.rb', line 94

def message_builder
  MessageBuilder.new
end

.producer_messages(producer_class) ⇒ Object

Get messages for a specific producer class



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lepus/testing.rb', line 70

def producer_messages(producer_class)
  return [] unless producer_class.respond_to?(:definition)

  begin
    exchange_name = producer_class.definition.exchange_name
    Exchange[exchange_name].messages
  rescue
    # If there's an error getting the exchange name, return empty array
    []
  end
end