Welcome to mirror list, hosted at ThFree Co, Russian Federation.

event_store.rb « matchers « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 14f6a42d7f488b6a82133f384c67972813f9d229 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true

RSpec::Matchers.define :publish_event do |expected_event_class|
  include RSpec::Matchers::Composable

  supports_block_expectations

  match do |proc|
    raise ArgumentError, 'This matcher only supports block expectation' unless proc.respond_to?(:call)

    @events ||= []

    allow(Gitlab::EventStore).to receive(:publish) do |published_event|
      @events << published_event
    end

    proc.call

    @events.any? do |event|
      event.instance_of?(expected_event_class) && match_data?(event.data, @expected_data)
    end
  end

  def match_data?(actual, expected)
    values_match?(actual.keys, expected.keys) &&
      actual.keys.each do |key|
        values_match?(actual[key], expected[key])
      end
  end

  chain :with do |expected_data|
    @expected_data = expected_data
  end

  failure_message do
    "expected #{expected_event_class} with #{@expected_data} to be published, but got #{@events}"
  end

  match_when_negated do |proc|
    raise ArgumentError, 'This matcher only supports block expectation' unless proc.respond_to?(:call)

    allow(Gitlab::EventStore).to receive(:publish)

    proc.call

    expect(Gitlab::EventStore).not_to have_received(:publish).with(instance_of(expected_event_class))
  end
end