Skip to content

Commit e0c6c78

Browse files
Fix expect_query helper for Ruby 3.3+
1 parent fd2a363 commit e0c6c78

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

spec/support/expectations.rb

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

43
module Mongoid
54
module Expectations
65
def expect_query(number)
76
if %i[ sharded load-balanced ].include?(ClusterConfig.instance.topology) && number > 0
87
skip 'This spec requires replica set or standalone topology'
98
end
10-
rv = nil
9+
1110
RSpec::Mocks.with_temporary_scope do
11+
klass = Mongo::Server::ConnectionBase
12+
1213
if number > 0
13-
expect_any_instance_of(Mongo::Server::ConnectionBase).to receive(:command_started).exactly(number).times.and_call_original
14+
# Due to changes in Ruby 3.3, RSpec's #and_call_original (which wraps the target
15+
# method) causes infinite recursion. We can achieve the same behavior with #bind.
16+
original_method = klass.instance_method(:command_started)
17+
expect_any_instance_of(klass).to receive(:command_started).exactly(number).times do |*args, **kwargs|
18+
instance = args.shift
19+
original_method.bind(instance).call(*args, **kwargs)
20+
end
1421
else
15-
expect_any_instance_of(Mongo::Server::ConnectionBase).not_to receive(:command_started)
22+
expect_any_instance_of(klass).not_to receive(:command_started)
1623
end
17-
rv = yield
24+
25+
yield
1826
end
19-
rv
2027
end
2128

2229
def expect_no_queries(&block)

0 commit comments

Comments
 (0)