Class: Lepus::Testing::RSpecMatchers::PublishLepusMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/lepus/testing/rspec_matchers.rb

Overview

RSpec matcher for testing message publishing

Instance Method Summary collapse

Constructor Details

#initialize(expected_count = nil) ⇒ PublishLepusMessage

Returns a new instance of PublishLepusMessage.



8
9
10
11
12
13
# File 'lib/lepus/testing/rspec_matchers.rb', line 8

def initialize(expected_count = nil)
  @expected_count = expected_count
  @expected_exchange = nil
  @expected_routing_key = nil
  @expected_payload = nil
end

Instance Method Details

#descriptionObject



69
70
71
# File 'lib/lepus/testing/rspec_matchers.rb', line 69

def description
  expected_description
end

#failure_messageObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lepus/testing/rspec_matchers.rb', line 43

def failure_message
  message = "expected to publish #{expected_description}"
  message += ", but published #{@published_count} message(s)"

  if @expected_exchange
    matching_exchange = get_recent_messages(@published_count).count { |msg| msg[:exchange] == @expected_exchange }
    message += " to exchange '#{@expected_exchange}' (#{matching_exchange} matched)"
  end

  if @expected_routing_key
    matching_routing = get_recent_messages(@published_count).count { |msg| msg[:routing_key] == @expected_routing_key }
    message += " with routing key '#{@expected_routing_key}' (#{matching_routing} matched)"
  end

  if @expected_payload
    matching_payload = get_recent_messages(@published_count).count { |msg| @expected_payload.matches?(msg[:payload]) }
    message += " with payload matching #{@expected_payload} (#{matching_payload} matched)"
  end

  message
end

#failure_message_when_negatedObject



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

def failure_message_when_negated
  "expected not to publish #{expected_description}, but did"
end

#matches?(actual = nil, &block) ⇒ Boolean

Supports both block expectations and value expectations with a producer class

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lepus/testing/rspec_matchers.rb', line 21

def matches?(actual = nil, &block)
  if block || actual.is_a?(Proc)
    @producer_class = nil
    @scoped_messages = nil

    @messages_before = count_all_messages
    (block || actual).call
    @messages_after = count_all_messages
    @published_count = @messages_after - @messages_before
  elsif actual.is_a?(Class) && actual < Lepus::Producer
    @producer_class = actual
    @scoped_messages = Lepus::Testing.producer_messages(@producer_class)
    @messages_before = 0
    @messages_after = @scoped_messages.size
    @published_count = @messages_after
  else
    return false
  end

  matches_count? && matches_exchange? && matches_routing_key? && matches_payload?
end

#supports_block_expectations?Boolean

Support block expectations

Returns:

  • (Boolean)


16
17
18
# File 'lib/lepus/testing/rspec_matchers.rb', line 16

def supports_block_expectations?
  true
end

#to_exchange(exchange_name) ⇒ Object

Chainable methods for specifying expectations



74
75
76
77
# File 'lib/lepus/testing/rspec_matchers.rb', line 74

def to_exchange(exchange_name)
  @expected_exchange = exchange_name.to_s
  self
end

#with(payload_matcher) ⇒ Object



84
85
86
87
# File 'lib/lepus/testing/rspec_matchers.rb', line 84

def with(payload_matcher)
  @expected_payload = payload_matcher
  self
end

#with_routing_key(routing_key) ⇒ Object



79
80
81
82
# File 'lib/lepus/testing/rspec_matchers.rb', line 79

def with_routing_key(routing_key)
  @expected_routing_key = routing_key.to_s
  self
end