Skip to content

Commit e5de3d9

Browse files
authored
Merge pull request #72 from ivx/improve-testhelper
Improve `Ears::Testing::TestHelper`
2 parents 462ba00 + d9e76ab commit e5de3d9

File tree

5 files changed

+71
-10
lines changed

5 files changed

+71
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 0.22.1 (2025-09-11)
4+
5+
- Add optional `routing_key_match` parameter to `#published_messages` in `Ears::Testing::TestHelper`
6+
- Add `#last_published_payload` to `Ears::Testing::TestHelper`
7+
38
## 0.22.0 (2025-09-11)
49

510
- Support publisher confirms in `Ears::Publisher`

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
ears (0.22.0)
4+
ears (0.22.1)
55
bunny (>= 2.22.0)
66
connection_pool (~> 2.4)
77
multi_json
@@ -113,7 +113,7 @@ CHECKSUMS
113113
connection_pool (2.5.3) sha256=cfd74a82b9b094d1ce30c4f1a346da23ee19dc8a062a16a85f58eab1ced4305b
114114
diff-lcs (1.6.2) sha256=9ae0d2cba7d4df3075fe8cd8602a8604993efc0dfa934cff568969efb1909962
115115
docile (1.4.1) sha256=96159be799bfa73cdb721b840e9802126e4e03dfc26863db73647204c727f21e
116-
ears (0.22.0)
116+
ears (0.22.1)
117117
json (2.13.2) sha256=02e1f118d434c6b230a64ffa5c8dee07e3ec96244335c392eaed39e1199dbb68
118118
language_server-protocol (3.17.0.5) sha256=fd1e39a51a28bf3eec959379985a72e296e9f9acfce46f6a79d31ca8760803cc
119119
lint_roller (1.1.0) sha256=2c0c845b632a7d172cb849cc90c1bce937a28c5c8ccccb50dfd46a485003cc87

lib/ears/testing/test_helper.rb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,38 @@ def ears_reset!
2828
end
2929
end
3030

31-
def published_messages(exchange_name = nil)
31+
def published_messages(exchange_name = nil, routing_key_match: nil)
3232
return [] unless Ears::Testing.message_capture
3333

34-
if exchange_name
35-
return Ears::Testing.message_capture.messages_for(exchange_name)
36-
end
34+
messages = exchange_name ? messages_for(exchange_name) : all_messages
35+
return messages unless routing_key_match
3736

38-
Ears::Testing.message_capture.all_messages
37+
messages.select do |message|
38+
message.routing_key.match?(routing_key_match)
39+
end
3940
end
4041

4142
def last_published_message(exchange_name = nil)
4243
published_messages(exchange_name).last
4344
end
4445

46+
def last_published_payload(exchange_name = nil)
47+
last_published_message(exchange_name).data
48+
end
49+
4550
def clear_published_messages
4651
Ears::Testing.message_capture&.clear
4752
end
53+
54+
private
55+
56+
def messages_for(exchange_name)
57+
Ears::Testing.message_capture.messages_for(exchange_name)
58+
end
59+
60+
def all_messages
61+
Ears::Testing.message_capture.all_messages
62+
end
4863
end
4964
end
5065
end

lib/ears/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Ears
2-
VERSION = '0.22.0'
2+
VERSION = '0.22.1'
33
end

spec/ears/testing/test_helper_spec.rb

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,18 @@
4848
Ears::Testing.message_capture.add_message('exchange1', 'data1', 'key1')
4949
Ears::Testing.message_capture.add_message('exchange2', 'data2', 'key2')
5050
Ears::Testing.message_capture.add_message('exchange1', 'data3', 'key3')
51+
Ears::Testing.message_capture.add_message('exchange2', 'data4', 'key3')
5152
end
5253

5354
it 'returns all messages when no exchange specified' do
5455
messages = helper.published_messages
55-
expect(messages.size).to eq(3)
56-
expect(messages.map(&:data)).to contain_exactly('data1', 'data2', 'data3')
56+
expect(messages.size).to eq(4)
57+
expect(messages.map(&:data)).to contain_exactly(
58+
'data1',
59+
'data2',
60+
'data3',
61+
'data4',
62+
)
5763
end
5864

5965
it 'returns messages for specific exchange' do
@@ -66,6 +72,26 @@
6672
helper.clear_published_messages
6773
expect(helper.published_messages).to eq([])
6874
end
75+
76+
context 'when routing key is passed' do
77+
it 'returns messages with specified routing key' do
78+
messages = helper.published_messages(routing_key_match: 'key3')
79+
expect(messages.size).to eq(2)
80+
expect(messages.map(&:data)).to contain_exactly('data3', 'data4')
81+
end
82+
end
83+
84+
context 'when routing key is passed as a regex' do
85+
it 'returns messages matching the specified routing key' do
86+
messages = helper.published_messages(routing_key_match: /^[^2]*$/)
87+
expect(messages.size).to eq(3)
88+
expect(messages.map(&:data)).to contain_exactly(
89+
'data1',
90+
'data3',
91+
'data4',
92+
)
93+
end
94+
end
6995
end
7096

7197
describe '#last_published_message' do
@@ -96,6 +122,21 @@
96122
end
97123
end
98124

125+
describe 'last_published_payload' do
126+
before do
127+
helper.mock_ears('exchange1')
128+
Ears::Testing.message_capture.add_message(
129+
'exchange1',
130+
'payload test',
131+
'key1',
132+
)
133+
end
134+
135+
it 'returns the payload of the last published message' do
136+
expect(helper.last_published_payload).to eq 'payload test'
137+
end
138+
end
139+
99140
describe '#clear_published_messages' do
100141
it 'clears all captured messages' do
101142
helper.mock_ears('exchange1')

0 commit comments

Comments
 (0)