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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# frozen_string_literal: true
require 'fast_spec_helper'
require 'json_schemer'
load File.expand_path('../../../spec/support/matchers/event_store.rb', __dir__)
RSpec.describe 'event store matchers', :aggregate_errors do
let(:event_type1) do
Class.new(Gitlab::EventStore::Event) do
def schema
{
'type' => 'object',
'properties' => {
'id' => { 'type' => 'integer' }
},
'required' => %w[id]
}
end
end
end
let(:event_type2) do
Class.new(Gitlab::EventStore::Event) do
def schema
{
'type' => 'object',
'properties' => {
'id' => { 'type' => 'integer' }
},
'required' => %w[id]
}
end
end
end
before do
stub_const('FakeEventType1', event_type1)
stub_const('FakeEventType2', event_type2)
end
def publishing_event(event_type, data = {})
::Gitlab::EventStore.publish(event_type.new(data: data))
end
describe 'publish_event' do
it 'requires a block matcher' do
matcher = -> { expect(:anything).to publish_event(:anything) } # rubocop: disable RSpec/ExpectActual
expect(&matcher).to raise_error(
ArgumentError,
'publish_event matcher only supports block expectation'
)
end
it 'validates the event type' do
valid_event_type = -> do
expect { publishing_event(FakeEventType1, { 'id' => 1 }) }
.to publish_event(FakeEventType1).with('id' => 1)
end
expect(&valid_event_type).not_to raise_error
invalid_event_type = -> do
expect { publishing_event(FakeEventType1, { 'id' => 1 }) }
.to publish_event(FakeEventType2).with('id' => 1)
end
expect(&invalid_event_type).to raise_error <<~MESSAGE
expected FakeEventType2 with {"id"=>1} to be published, but only the following events were published:
- FakeEventType1 with {"id"=>1}
MESSAGE
end
it 'validates the event data' do
missing_data = -> do
expect { publishing_event(FakeEventType1, { 'id' => 1 }) }
.to publish_event(FakeEventType1)
end
expect(&missing_data).to raise_error <<~MESSAGE
expected FakeEventType1 with no data to be published, but only the following events were published:
- FakeEventType1 with {"id"=>1}
MESSAGE
different_data = -> do
expect { publishing_event(FakeEventType1, { 'id' => 1 }) }
.to publish_event(FakeEventType1).with({ 'id' => 2 })
end
expect(&different_data).to raise_error <<~MESSAGE
expected FakeEventType1 with {"id"=>2} to be published, but only the following events were published:
- FakeEventType1 with {"id"=>1}
MESSAGE
end
end
describe 'not_publish_event' do
it 'requires a block matcher' do
matcher = -> { expect(:anything).to not_publish_event(:anything) } # rubocop: disable RSpec/ExpectActual
expect(&matcher)
.to raise_error(ArgumentError, 'not_publish_event matcher only supports block expectation')
end
it 'does not permit .with' do
matcher = -> do
expect { publishing_event(FakeEventType1, { 'id' => 1 }) }
.to not_publish_event(FakeEventType2).with({ 'id' => 1 })
end
expect(&matcher)
.to raise_error(ArgumentError, 'not_publish_event does not permit .with to avoid ambiguity')
end
it 'validates the event type' do
matcher = -> do
expect { publishing_event(FakeEventType1, { 'id' => 1 }) }
.to not_publish_event(FakeEventType1)
end
expect(&matcher)
.to raise_error('expected FakeEventType1 not to be published')
end
end
end
|