Skip to content

Commit 3234068

Browse files
committed
Fix test isolation issue in publisher spec
Reset Connection singleton between tests to prevent mock leakage. The Connection singleton was caching the mocked JetStream context across test examples, causing RSpec to detect double leakage when tests ran in certain orders. Fixes issue where 'persists exception when publish raises error' test would fail with 'Double was originally created in one example but has leaked into another example' error.
1 parent 7304500 commit 3234068

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

spec/publisher/publisher_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010

1111
before do
1212
JetstreamBridge.reset!
13+
# Reset Connection singleton to prevent mock leakage between tests
14+
JetstreamBridge::Connection.instance_variable_set(:@singleton__instance__, nil)
1315
# Mock Connection.connect! before configure to prevent actual connection
1416
allow(JetstreamBridge::Connection).to receive(:connect!).and_return(jts)
1517
JetstreamBridge.configure do |c|
1618
c.destination_app = 'dest'
1719
c.app_name = 'source'
1820
c.env = 'test'
1921
end
20-
allow(jts).to receive(:publish).and_return(ack)
22+
allow(jts).to receive(:publish) { ack }
2123
end
2224

2325
after { JetstreamBridge.reset! }
@@ -268,7 +270,7 @@
268270
end
269271

270272
it 'persists exception when publish raises error' do
271-
allow(jts).to receive(:publish).and_raise(StandardError, 'NATS error')
273+
allow(jts).to receive(:publish) { raise StandardError, 'NATS error' }
272274
expect(outbox_repo).to receive(:persist_exception).with(record, kind_of(StandardError))
273275

274276
result = publisher.publish(resource_type: 'user', event_type: 'created', payload: payload)

0 commit comments

Comments
 (0)