Skip to content

Commit fe8cbfa

Browse files
johnnyshieldsjamis
authored andcommitted
MONGOID-5872 - [🛠️ Let's Fix Mongoid's Broken CI] Fix #expect_query test helper for Ruby 3.3+ (mongodb#5981)
Co-authored-by: Jamis Buck <[email protected]>
1 parent 437501d commit fe8cbfa

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

‎spec/support/expectations.rb

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

43
module Mongoid
54
module Expectations
6-
7-
def connection_class
8-
if defined?(Mongo::Server::ConnectionBase)
9-
Mongo::Server::ConnectionBase
10-
else
11-
# Pre-2.8 drivers
12-
Mongo::Server::Connection
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.
7+
def expect_query(number)
8+
if %i[ sharded load-balanced ].include?(ClusterConfig.instance.topology) && number > 0
9+
skip 'This spec requires replica set or standalone topology'
1310
end
14-
end
1511

16-
def expect_query(number)
17-
rv = nil
18-
RSpec::Mocks.with_temporary_scope do
19-
if number > 0
20-
expect_any_instance_of(connection_class).to receive(:command_started).exactly(number).times.and_call_original
21-
else
22-
expect_any_instance_of(connection_class).not_to receive(:command_started)
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)
2320
end
24-
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)
2528
end
26-
rv
2729
end
2830

2931
def expect_no_queries(&block)

0 commit comments

Comments
 (0)