File tree 1 file changed +18
-18
lines changed
1 file changed +18
-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
- 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.
15
7
def expect_query ( number )
16
8
if %i[ sharded load-balanced ] . include? ( ClusterConfig . instance . topology ) && number > 0
17
9
skip 'This spec requires replica set or standalone topology'
18
10
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 )
25
20
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 )
27
28
end
28
- rv
29
29
end
30
30
31
31
def expect_no_queries ( &block )
You can’t perform that action at this time.
0 commit comments