File: configuration.rb

package info (click to toggle)
ruby-mocha 2.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,540 kB
  • sloc: ruby: 11,899; javascript: 477; makefile: 14
file content (354 lines) | stat: -rw-r--r-- 13,226 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
require 'mocha/ruby_version'

module Mocha
  # Allows setting of configuration options. See {Configuration} for the available options.
  #
  # Typically the configuration is set globally in a +test_helper.rb+ or +spec_helper.rb+ file.
  #
  # @see Configuration
  #
  # @yieldparam configuration [Configuration] the configuration for modification
  #
  # @example Setting multiple configuration options
  #   Mocha.configure do |c|
  #     c.stubbing_method_unnecessarily = :prevent
  #     c.stubbing_method_on_non_mock_object = :warn
  #     c.stubbing_method_on_nil = :allow
  #   end
  #
  def self.configure
    yield configuration
  end

  # @private
  def self.configuration
    Configuration.configuration
  end

  # This class provides a number of ways to configure the library.
  #
  # Typically the configuration is set globally in a +test_helper.rb+ or +spec_helper.rb+ file.
  #
  # @example Setting multiple configuration options
  #   Mocha.configure do |c|
  #     c.stubbing_method_unnecessarily = :prevent
  #     c.stubbing_method_on_non_mock_object = :warn
  #     c.stubbing_method_on_nil = :allow
  #   end
  #
  class Configuration
    # @private
    DEFAULTS = {
      stubbing_method_unnecessarily: :allow,
      stubbing_method_on_non_mock_object: :allow,
      stubbing_non_existent_method: :allow,
      stubbing_non_public_method: :allow,
      stubbing_method_on_nil: :prevent,
      display_matching_invocations_on_failure: false,
      strict_keyword_argument_matching: false
    }.freeze

    attr_reader :options
    protected :options

    # @private
    def initialize(options = {})
      @options = DEFAULTS.merge(options)
    end

    # @private
    def initialize_copy(other)
      @options = other.options.dup
    end

    # @private
    def merge(other)
      self.class.new(@options.merge(other.options))
    end

    # Configure whether stubbing methods unnecessarily is allowed.
    #
    # This is useful for identifying unused stubs. Unused stubs are often accidentally introduced when code is {http://martinfowler.com/bliki/DefinitionOfRefactoring.html refactored}.
    #
    # When +value+ is +:allow+, do nothing. This is the default.
    # When +value+ is +:warn+, display a warning.
    # When +value+ is +:prevent+, raise a {StubbingError}.
    #
    # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
    #
    # @example Preventing unnecessary stubbing of a method
    #   Mocha.configure do |c|
    #     c.stubbing_method_unnecessarily = :prevent
    #   end
    #
    #   example = mock('example')
    #   example.stubs(:unused_stub)
    #   # => Mocha::StubbingError: stubbing method unnecessarily:
    #   # =>   #<Mock:example>.unused_stub(any_parameters)
    #
    def stubbing_method_unnecessarily=(value)
      @options[:stubbing_method_unnecessarily] = value
    end

    # @private
    def stubbing_method_unnecessarily
      @options[:stubbing_method_unnecessarily]
    end

    # Configure whether stubbing methods on non-mock objects is allowed.
    #
    # If you like the idea of {http://www.jmock.org/oopsla2004.pdf mocking roles not objects} and {http://www.mockobjects.com/2007/04/test-smell-mocking-concrete-classes.html you don't like stubbing concrete classes}, this is the setting for you. However, while this restriction makes a lot of sense in Java with its {http://java.sun.com/docs/books/tutorial/java/concepts/interface.html explicit interfaces}, it may be moot in Ruby where roles are probably best represented as Modules.
    #
    # When +value+ is +:allow+, do nothing. This is the default.
    # When +value+ is +:warn+, display a warning.
    # When +value+ is +:prevent+, raise a {StubbingError}.
    #
    # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
    #
    # @example Preventing stubbing of a method on a non-mock object
    #   Mocha.configure do |c|
    #     c.stubbing_method_on_non_mock_object = :prevent
    #   end
    #
    #   class Example
    #     def example_method; end
    #   end
    #
    #   example = Example.new
    #   example.stubs(:example_method)
    #   # => Mocha::StubbingError: stubbing method on non-mock object:
    #   # =>   #<Example:0x593620>.example_method
    #
    def stubbing_method_on_non_mock_object=(value)
      @options[:stubbing_method_on_non_mock_object] = value
    end

    # @private
    def stubbing_method_on_non_mock_object
      @options[:stubbing_method_on_non_mock_object]
    end

    # Configure whether stubbing of non-existent methods is allowed.
    #
    # This is useful if you want to ensure that methods you're mocking really exist. A common criticism of unit tests with mock objects is that such a test may (incorrectly) pass when an equivalent non-mocking test would (correctly) fail. While you should always have some integration tests, particularly for critical business functionality, this Mocha configuration setting should catch scenarios when mocked methods and real methods have become misaligned.
    #
    # When +value+ is +:allow+, do nothing. This is the default.
    # When +value+ is +:warn+, display a warning.
    # When +value+ is +:prevent+, raise a {StubbingError}.
    #
    # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
    #
    # @example Preventing stubbing of a non-existent method
    #
    #   Mocha.configure do |c|
    #     c.stubbing_non_existent_method = :prevent
    #   end
    #
    #   class Example
    #   end
    #
    #   example = Example.new
    #   example.stubs(:method_that_doesnt_exist)
    #   # => Mocha::StubbingError: stubbing non-existent method:
    #   # =>   #<Example:0x593760>.method_that_doesnt_exist
    #
    def stubbing_non_existent_method=(value)
      @options[:stubbing_non_existent_method] = value
    end

    # @private
    def stubbing_non_existent_method
      @options[:stubbing_non_existent_method]
    end

    # Configure whether stubbing of non-public methods is allowed.
    #
    # Many people think that it's good practice only to mock public methods. This is one way to prevent your tests being too tightly coupled to the internal implementation of a class. Such tests tend to be very brittle and not much use when refactoring.
    #
    # When +value+ is +:allow+, do nothing. This is the default.
    # When +value+ is +:warn+, display a warning.
    # When +value+ is +:prevent+, raise a {StubbingError}.
    #
    # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
    #
    # @example Preventing stubbing of a non-public method
    #   Mocha.configure do |c|
    #     c.stubbing_non_public_method = :prevent
    #   end
    #
    #   class Example
    #     def internal_method; end
    #     private :internal_method
    #   end
    #
    #   example = Example.new
    #   example.stubs(:internal_method)
    #   # => Mocha::StubbingError: stubbing non-public method:
    #   # =>   #<Example:0x593530>.internal_method
    #
    def stubbing_non_public_method=(value)
      @options[:stubbing_non_public_method] = value
    end

    # @private
    def stubbing_non_public_method
      @options[:stubbing_non_public_method]
    end

    # Configure whether stubbing methods on the +nil+ object is allowed.
    #
    # This is usually done accidentally, but there might be rare cases where it is intended.
    #
    # This option only works for Ruby < v2.2.0. In later versions of Ruby +nil+ is frozen and so a {StubbingError} will be raised if you attempt to stub a method on +nil+.
    #
    # When +value+ is +:allow+, do nothing.
    # When +value+ is +:warn+, display a warning.
    # When +value+ is +:prevent+, raise a {StubbingError}. This is the default.
    #
    # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
    #
    def stubbing_method_on_nil=(value)
      @options[:stubbing_method_on_nil] = value
    end

    # @private
    def stubbing_method_on_nil
      @options[:stubbing_method_on_nil]
    end

    # Display matching invocations alongside expectations on Mocha-related test failure.
    #
    # @param [Boolean] value +true+ to enable display of matching invocations; disabled by default.
    #
    # @example Enable display of matching invocations
    #   Mocha.configure do |c|
    #     c.display_matching_invocations_on_failure = true
    #   end
    #
    #   foo = mock('foo')
    #   foo.expects(:bar)
    #   foo.stubs(:baz).returns('baz').raises(RuntimeError).throws(:tag, 'value')
    #
    #   foo.baz(1, 2)
    #   assert_raises(RuntimeError) { foo.baz(3, 4) }
    #   assert_throws(:tag) { foo.baz(5, 6) }
    #
    #   not all expectations were satisfied
    #   unsatisfied expectations:
    #   - expected exactly once, invoked never: #<Mock:foo>.bar
    #   satisfied expectations:
    #   - allowed any number of times, invoked 3 times: #<Mock:foo>.baz(any_parameters)
    #     - #<Mock:foo>.baz(1, 2) # => "baz"
    #     - #<Mock:foo>.baz(3, 4) # => raised RuntimeError
    #     - #<Mock:foo>.baz(5, 6) # => threw (:tag, "value")
    def display_matching_invocations_on_failure=(value)
      @options[:display_matching_invocations_on_failure] = value
    end

    # @private
    def display_matching_invocations_on_failure?
      @options[:display_matching_invocations_on_failure]
    end

    # Perform strict keyword argument comparison. Only supported in Ruby >= v2.7.
    #
    # When this option is set to +false+ a positional +Hash+ and a set of keyword arguments are treated the same during comparison, which can lead to misleading passing tests in Ruby >= v3.0 (see examples below). However, a deprecation warning will be displayed if a positional +Hash+ matches a set of keyword arguments or vice versa. This is because {#strict_keyword_argument_matching=} will default to +true+ in the future.
    #
    # For more details on keyword arguments in Ruby v3, refer to {https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0 this article}.
    #
    # Note that +Hash+-related matchers such as {ParameterMatchers#has_value} or {ParameterMatchers#has_key} will still treat a positional +Hash+ and a set of keyword arguments the same, so misleading passing tests are still possible when they are used.
    #
    # This configuration option is +false+ by default to enable gradual adoption, but will be +true+ by default in the future.
    #
    # @param [Boolean] value +true+ to enable strict keyword argument matching; +false+ by default.
    #
    # @example Loose keyword argument matching (default)
    #
    #   class Example
    #     def foo(a, bar:); end
    #   end
    #
    #   example = Example.new
    #   example.expects(:foo).with('a', bar: 'b')
    #   example.foo('a', { bar: 'b' })
    #   # This passes the test, but would result in an ArgumentError in practice
    #
    # @example Strict keyword argument matching
    #
    #   Mocha.configure do |c|
    #     c.strict_keyword_argument_matching = true
    #   end
    #
    #   class Example
    #     def foo(a, bar:); end
    #   end
    #
    #   example = Example.new
    #   example.expects(:foo).with('a', bar: 'b')
    #   example.foo('a', { bar: 'b' })
    #   # This now fails as expected
    def strict_keyword_argument_matching=(value)
      raise 'Strict keyword argument matching requires Ruby 2.7 and above.' unless Mocha::RUBY_V27_PLUS
      @options[:strict_keyword_argument_matching] = value
    end

    # @private
    def strict_keyword_argument_matching?
      @options[:strict_keyword_argument_matching]
    end

    class << self
      # @private
      def reset_configuration
        @configuration = nil
      end

      # Temporarily modify {Configuration} options.
      #
      # The supplied +temporary_options+ will override the current configuration for the duration of the supplied block.
      # The configuration will be returned to its original state when the block returns.
      #
      # @param [Hash] temporary_options the configuration options to apply for the duration of the block.
      # @yield block during which the configuration change will be in force.
      #
      # @example Temporarily allow stubbing of +nil+
      #   Mocha::Configuration.override(stubbing_method_on_nil: :allow) do
      #     nil.stubs(:foo)
      #   end
      def override(temporary_options)
        original_configuration = configuration
        @configuration = configuration.merge(new(temporary_options))
        yield
      ensure
        @configuration = original_configuration
      end

      # @private
      def configuration
        @configuration ||= new
      end

      private

      # @private
      def change_config(action, new_value, &block)
        if block_given?
          temporarily_change_config action, new_value, &block
        else
          configuration.send("#{action}=".to_sym, new_value)
        end
      end

      # @private
      def temporarily_change_config(action, new_value)
        original_configuration = configuration
        new_configuration = configuration.dup
        new_configuration.send("#{action}=".to_sym, new_value)
        @configuration = new_configuration
        yield
      ensure
        @configuration = original_configuration
      end
    end
  end
end