File tree Expand file tree Collapse file tree 1 file changed +20
-18
lines changed Expand file tree Collapse file tree 1 file changed +20
-18
lines changed Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
- # rubocop:todo all
3
2
4
3
module Mongoid
5
4
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'
13
10
end
14
- end
15
11
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 )
23
20
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 )
25
28
end
26
- rv
27
29
end
28
30
29
31
def expect_no_queries ( &block )
You can’t perform that action at this time.
0 commit comments