mocha 2.6.1 → 2.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 427e4fdcdcceac0496dd9ad52802ae29be41ba102de0888e58f6c379c77ac24e
4
- data.tar.gz: d2c3e87c6d5f861f1ae2124b758fd548e19f2299e1ebe69551b7483d0941516d
3
+ metadata.gz: 61f3a7bc2e73c16b715a31472883ed85fc55726d28e43d051b5d62da21362b52
4
+ data.tar.gz: 8e2b6b411543932dfb02f9d97cbe0eb35ed48e983e9079a426c7963f075416da
5
5
  SHA512:
6
- metadata.gz: 7b82f53427cf92b742d97c551808e42f8b954148f7311f97bb52e4c1c15952515cce115e23022ce10a5d52e07238dc8ce7188d810de5f4a7e193ca59d72b2e4f
7
- data.tar.gz: c345496f74391ca1b8b2f6a4b0d3947f30c119a89416c7f69037b064c6ca207a77607fa8bd93443152c196f4f5fef93cccc984c4a75040ee675d140237dd0f8b
6
+ metadata.gz: dd352ea7b0ef64b7e56880a90faecd452b67891765a23441013f73405546da1746314025bb4c556b1322ea621a7d28d940efc77272a52b63716993bc27ebe94f
7
+ data.tar.gz: e229f55ddd4d53350941bef0af9d3b1026a76f904b666d187ebd6d808feb284101677c66e47005ea3889767626e1cd556330e222d80752f57be89665d4fafd3a
data/Gemfile CHANGED
@@ -25,6 +25,10 @@ if RUBY_VERSION >= '2.2.0'
25
25
  gem 'jaro_winkler', '>= 1.5.5'
26
26
  gem 'rubocop', '> 0.56', '<= 0.58.2'
27
27
  end
28
+ if RUBY_ENGINE == 'jruby'
29
+ # Workaround for https://github.com/jruby/jruby/issues/8488
30
+ gem 'jar-dependencies', '~> 0.4.1'
31
+ end
28
32
  if ENV['MOCHA_GENERATE_DOCS']
29
33
  gem 'redcarpet'
30
34
  gem 'yard'
data/README.md CHANGED
@@ -8,6 +8,7 @@
8
8
  * Supported by many other test frameworks.
9
9
 
10
10
  ### Intended Usage
11
+
11
12
  Mocha is intended to be used in unit tests for the [Mock Object](http://xunitpatterns.com/Mock%20Object.html) or [Test Stub](http://xunitpatterns.com/Test%20Stub.html) types of [Test Double](http://xunitpatterns.com/Test%20Double.html), not the [Fake Object](http://xunitpatterns.com/Fake%20Object.html) or [Test Spy](http://xunitpatterns.com/Test%20Spy.html) types. Although it would be possible to extend Mocha to allow the implementation of fakes and spies, we have chosen to keep it focused on mocks and stubs.
12
13
 
13
14
  ### Installation
@@ -276,7 +277,7 @@ Maybe, but probably not. Partial mocking changes the state of objects in the `Ob
276
277
 
277
278
  Stubs and expectations are basically the same thing. A stub is just an expectation of zero or more invocations. The `Expectation#stubs` method is syntactic sugar to make the intent of the test more explicit.
278
279
 
279
- When a method is invoked on a mock object, the mock object searches through its expectations from newest to oldest to find one that matches the invocation. After the invocation, the matching expectation might stop matching further invocations.
280
+ When a method is invoked on a mock object, the mock object searches through its expectations from newest to oldest to find one that matches the invocation. After the invocation, the matching expectation might stop matching further invocations. If the expectation that matches the invocation has a cardinality of "never", then an unexpected invocation error is reported.
280
281
 
281
282
  See the [documentation](https://mocha.jamesmead.org/Mocha/Mock.html) for `Mocha::Mock` for further details.
282
283
 
data/RELEASE.md CHANGED
@@ -1,5 +1,34 @@
1
1
  # Release Notes
2
2
 
3
+ ## 2.7.1
4
+
5
+ ### External changes
6
+
7
+ * Deprecate `Configuration#stubbing_method_on_nil=` (#694)
8
+ * Indicate when parameter matcher logic is defined by block passed to `Expectation#with` (#698, b30e4434)
9
+ * Improve documentation for `Expectation#with`, especially when it is passed a block (#698, #682, #606 & #681)
10
+
11
+ ## 2.7.0
12
+
13
+ ### External changes
14
+
15
+ * Fail fast if invocation matches never expectation (#679, #678, #490, #131 & #44) - thanks to @ducmtran & @davidstosik for reporting
16
+
17
+ ### Internal changes
18
+
19
+ * Workaround for JRuby jar-dependencies issue (#690)
20
+ * Ruby v3.4 stacktrace uses single-quote vs backtick (#688 & #689) - thanks to Vít Ondruch
21
+
22
+ **WARNING: This release fixes a very _old_ bug**
23
+ * The bug relates to the use of `Expectation#never` in combination with other expectations on the same method.
24
+ * Please ensure you fix the relevant deprecation warnings when running against v2.6.1 *before* upgrading to v2.7.0.
25
+ * Previously, the following test would have passed, but now it will fail with an unexpected invocation error on the `foo.bar` line.
26
+
27
+ foo = mock('foo')
28
+ foo.stubs(:bar)
29
+ foo.expects(:bar).never
30
+ foo.bar
31
+
3
32
  ## 2.6.1
4
33
 
5
34
  ### External changes
@@ -1,4 +1,5 @@
1
1
  require 'mocha/ruby_version'
2
+ require 'mocha/deprecation'
2
3
 
3
4
  module Mocha
4
5
  # Allows setting of configuration options. See {Configuration} for the available options.
@@ -206,8 +207,14 @@ module Mocha
206
207
  # When +value+ is +:prevent+, raise a {StubbingError}. This is the default.
207
208
  #
208
209
  # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
210
+ # @deprecated This method is deprecated and will be removed in a future release. +nil+ is frozen in Ruby >= v2.2 and Mocha will be dropping support for Ruby v2.1. At that point it won't be possible to stub methods on +nil+ any more.
209
211
  #
210
212
  def stubbing_method_on_nil=(value)
213
+ Deprecation.warning([
214
+ '`Mocha::Configuration#stubbing_method_on_nil=` is deprecated and will be removed in a future release.',
215
+ '`nil` is frozen in Ruby >= v2.2 and Mocha will be dropping support for Ruby v2.1.',
216
+ "At that point it won't be possible to stub methods on `nil` any more."
217
+ ].join(' '))
211
218
  @options[:stubbing_method_on_nil] = value
212
219
  end
213
220
 
@@ -193,6 +193,11 @@ module Mocha
193
193
  #
194
194
  # May be used with Ruby literals or variables for exact matching or with parameter matchers for less-specific matching, e.g. {ParameterMatchers#includes}, {ParameterMatchers#has_key}, etc. See {ParameterMatchers} for a list of all available parameter matchers.
195
195
  #
196
+ # Alternatively a block argument can be passed to {#with} to implement custom parameter matching. The block receives the +*actual_parameters+ as its arguments and should return +true+ if they are acceptable or +false+ otherwise. See the example below where a method is expected to be called with a value divisible by 4.
197
+ # The block argument takes precedence over +expected_parameters_or_matchers+. The block may be called multiple times per invocation of the expected method and so it should be idempotent.
198
+ #
199
+ # Note that if {#with} is called multiple times on the same expectation, the last call takes precedence; other calls are ignored.
200
+ #
196
201
  # Positional arguments were separated from keyword arguments in Ruby v3 (see {https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0 this article}). In relation to this a new configuration option ({Configuration#strict_keyword_argument_matching=}) is available in Ruby >= 2.7.
197
202
  #
198
203
  # When {Configuration#strict_keyword_argument_matching=} is set to +false+ (which is currently the default), a positional +Hash+ and a set of keyword arguments passed to {#with} are treated the same for the purposes of parameter matching. However, a deprecation warning will be displayed if a positional +Hash+ matches a set of keyword arguments or vice versa. This is because {Configuration#strict_keyword_argument_matching=} will default to +true+ in the future.
@@ -202,10 +207,10 @@ module Mocha
202
207
  # @see ParameterMatchers
203
208
  # @see Configuration#strict_keyword_argument_matching=
204
209
  #
205
- # @param [*Array<Object,ParameterMatchers::Base>] expected_parameters_or_matchers expected parameter values or parameter matchers.
210
+ # @param [Array<Object,ParameterMatchers::Base>] expected_parameters_or_matchers expected parameter values or parameter matchers.
206
211
  # @yield optional block specifying custom matching.
207
- # @yieldparam [*Array<Object>] actual_parameters parameters with which expected method was invoked.
208
- # @yieldreturn [Boolean] +true+ if +actual_parameters+ are acceptable.
212
+ # @yieldparam [Array<Object>] actual_parameters parameters with which expected method was invoked.
213
+ # @yieldreturn [Boolean] +true+ if +actual_parameters+ are acceptable; +false+ otherwise.
209
214
  # @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
210
215
  #
211
216
  # @example Expected method must be called with exact parameter values.
@@ -256,7 +261,7 @@ module Mocha
256
261
  # example.foo('a', { bar: 'b' })
257
262
  # # This now fails as expected
258
263
  #
259
- # @example Expected method must be called with a value divisible by 4.
264
+ # @example Using a block argument to expect the method to be called with a value divisible by 4.
260
265
  # object = mock()
261
266
  # object.expects(:expected_method).with() { |value| value % 4 == 0 }
262
267
  # object.expected_method(16)
@@ -266,6 +271,22 @@ module Mocha
266
271
  # object.expects(:expected_method).with() { |value| value % 4 == 0 }
267
272
  # object.expected_method(17)
268
273
  # # => verify fails
274
+ #
275
+ # @example Extracting a custom matcher into an instance method on the test class.
276
+ # class MyTest < Minitest::Test
277
+ # def test_expected_method_is_called_with_a_value_divisible_by_4
278
+ # object = mock()
279
+ # object.expects(:expected_method).with(&method(:divisible_by_4))
280
+ # object.expected_method(16)
281
+ # # => verify succeeds
282
+ # end
283
+ #
284
+ # private
285
+ #
286
+ # def divisible_by_4(value)
287
+ # value % 4 == 0
288
+ # end
289
+ # end
269
290
  def with(*expected_parameters_or_matchers, &matching_block)
270
291
  @parameters_matcher = ParametersMatcher.new(expected_parameters_or_matchers, self, &matching_block)
271
292
  self
data/lib/mocha/mock.rb CHANGED
@@ -35,6 +35,9 @@ module Mocha
35
35
  # while an +expects(:foo).at_least_once+ expectation will always be matched
36
36
  # against invocations.
37
37
  #
38
+ # However, note that if the expectation that matches the invocation has a
39
+ # cardinality of "never", then an unexpected invocation error is reported.
40
+ #
38
41
  # This scheme allows you to:
39
42
  #
40
43
  # - Set up default stubs in your the +setup+ method of your test class and
@@ -326,15 +329,11 @@ module Mocha
326
329
  matching_expectations = all_expectations.matching_expectations(invocation)
327
330
 
328
331
  index = 0
329
- never_allowed_expectation = nil
330
332
  while index < matching_expectations.length
331
333
  matching_expectation = matching_expectations[index]
332
334
  if matching_expectation.invocations_never_allowed?
333
- never_allowed_expectation = matching_expectation
335
+ raise_unexpected_invocation_error(invocation, matching_expectation)
334
336
  elsif matching_expectation.invocations_allowed?
335
- if never_allowed_expectation
336
- invocation_not_allowed_warning(invocation, never_allowed_expectation)
337
- end
338
337
  return matching_expectation.invoke(invocation)
339
338
  end
340
339
  index += 1
@@ -387,14 +386,6 @@ module Mocha
387
386
 
388
387
  private
389
388
 
390
- def invocation_not_allowed_warning(invocation, expectation)
391
- messages = [
392
- "The expectation defined at #{expectation.definition_location} does not allow invocations, but #{invocation.call_description} was invoked.",
393
- 'This invocation will cause the test to fail fast in a future version of Mocha.'
394
- ]
395
- Deprecation.warning(messages.join(' '))
396
- end
397
-
398
389
  def raise_unexpected_invocation_error(invocation, matching_expectation)
399
390
  if @unexpected_invocation.nil?
400
391
  @unexpected_invocation = invocation
@@ -22,9 +22,13 @@ module Mocha
22
22
  end
23
23
 
24
24
  def mocha_inspect
25
- signature = matchers.mocha_inspect
26
- signature = signature.gsub(/^\[|\]$/, '')
27
- "(#{signature})"
25
+ if @matching_block
26
+ '(arguments_accepted_by_custom_matching_block)'
27
+ else
28
+ signature = matchers.mocha_inspect
29
+ signature = signature.gsub(/^\[|\]$/, '')
30
+ "(#{signature})"
31
+ end
28
32
  end
29
33
 
30
34
  def matchers
data/lib/mocha/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Mocha
2
- VERSION = '2.6.1'.freeze
2
+ VERSION = '2.7.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mocha
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.1
4
+ version: 2.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Mead
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-28 00:00:00.000000000 Z
11
+ date: 2024-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby2_keywords