Skip to content

Commit 5227859

Browse files
johnnyshieldsjamis
andauthored
MONGOID-5872 - [🛠️ Let's Fix Mongoid's Broken CI] Fix #expect_query test helper for Ruby 3.3+ (#5981)
Co-authored-by: Jamis Buck <jamis.buck@mongodb.com>
1 parent 8e3c299 commit 5227859

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

‎spec/support/expectations.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
# frozen_string_literal: true
2-
# rubocop:todo all
32

43
module Mongoid
54
module Expectations
6-
def connection_class
7-
if defined?(Mongo::Server::ConnectionBase)
8-
Mongo::Server::ConnectionBase
9-
else
10-
# Pre-2.8 drivers
11-
Mongo::Server::Connection
12-
end
13-
end
14-
5+
# Previously this method used RSpec::Mocks with .exactly.times(n).and_call_original,
6+
# which stopped working reliably in Ruby 3.3. Now we directly wrap the target method.
157
def expect_query(number)
168
if %i[ sharded load-balanced ].include?(ClusterConfig.instance.topology) && number > 0
179
skip 'This spec requires replica set or standalone topology'
1810
end
19-
rv = nil
20-
RSpec::Mocks.with_temporary_scope do
21-
if number > 0
22-
expect_any_instance_of(connection_class).to receive(:command_started).exactly(number).times.and_call_original
23-
else
24-
expect_any_instance_of(connection_class).not_to receive(:command_started)
11+
12+
klass = Mongo::Server::ConnectionBase
13+
original_method = klass.instance_method(:command_started)
14+
query_count = 0
15+
16+
begin
17+
klass.define_method(:command_started) do |*args, **kwargs|
18+
query_count += 1
19+
original_method.bind_call(self, *args, **kwargs)
2520
end
26-
rv = yield
21+
22+
result = yield
23+
expect(query_count).to eq(number)
24+
result
25+
ensure
26+
klass.remove_method(:command_started)
27+
klass.define_method(:command_started, original_method)
2728
end
28-
rv
2929
end
3030

3131
def expect_no_queries(&block)

0 commit comments

Comments
 (0)