mocha 1.4.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +12 -71
  3. data/RELEASE.md +12 -0
  4. data/lib/mocha/central.rb +17 -0
  5. data/lib/mocha/class_methods.rb +6 -2
  6. data/lib/mocha/configuration.rb +66 -1
  7. data/lib/mocha/error_with_filtered_backtrace.rb +17 -0
  8. data/lib/mocha/hooks.rb +1 -2
  9. data/lib/mocha/integration/mini_test/version_13.rb +1 -0
  10. data/lib/mocha/integration/mini_test/version_140.rb +1 -0
  11. data/lib/mocha/integration/mini_test/version_141.rb +1 -0
  12. data/lib/mocha/integration/mini_test/version_142_to_172.rb +1 -0
  13. data/lib/mocha/integration/mini_test/version_200.rb +1 -0
  14. data/lib/mocha/integration/mini_test/version_201_to_222.rb +1 -0
  15. data/lib/mocha/integration/mini_test/version_2110_to_2111.rb +1 -0
  16. data/lib/mocha/integration/mini_test/version_2112_to_320.rb +1 -0
  17. data/lib/mocha/integration/mini_test/version_230_to_2101.rb +1 -0
  18. data/lib/mocha/integration/test_unit/gem_version_200.rb +1 -0
  19. data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +1 -0
  20. data/lib/mocha/integration/test_unit/gem_version_203_to_220.rb +1 -0
  21. data/lib/mocha/integration/test_unit/gem_version_230_to_250.rb +1 -0
  22. data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +1 -0
  23. data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +1 -0
  24. data/lib/mocha/mockery.rb +38 -3
  25. data/lib/mocha/module_method.rb +0 -8
  26. data/lib/mocha/not_initialized_error.rb +9 -0
  27. data/lib/mocha/object_methods.rb +6 -2
  28. data/lib/mocha/receivers.rb +4 -2
  29. data/lib/mocha/stubbing_error.rb +2 -11
  30. data/lib/mocha/version.rb +1 -1
  31. data/test/acceptance/acceptance_test_helper.rb +0 -2
  32. data/test/acceptance/prevent_use_of_mocha_outside_test_test.rb +79 -0
  33. data/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb +31 -1
  34. data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +30 -0
  35. data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +63 -0
  36. data/test/unit/class_methods_test.rb +31 -0
  37. data/test/unit/hooks_test.rb +5 -4
  38. data/test/unit/mockery_test.rb +17 -2
  39. data/test/unit/object_methods_test.rb +19 -1
  40. data/test/unit/receivers_test.rb +31 -3
  41. metadata +5 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b1a4df2eb13604de6b309bc453307d375c055620
4
- data.tar.gz: 9144ebfe812beea562fc8c9ebdfb87326cb5ffa6
3
+ metadata.gz: c4c3e82b675148ae87d3cb16bd97816bc04b04b3
4
+ data.tar.gz: 888491a9247d93053283fbfa0b7a59d073beb760
5
5
  SHA512:
6
- metadata.gz: 1cf5b923bb4a91c6b3a1dac90aca3084229955743ae64c41cbecf561e8fd572f419d09d155ff3c9b6d0616258ca526d70677d79f9861e8987f99ee5b312bbb90
7
- data.tar.gz: 9e635e0f0897d0437acffe0cfe4755ecae028da76513445a5b4b56ee48be878ae0070fa24d3fc8bddad02a69d790223909b936fdcdb6f53114feeec8c69cd3c4
6
+ metadata.gz: dc6cbdc1710ee63e7d627fa2b73e62fc6a175d743a9d57c81d314a7c86d167ba4b719d7f5345438eb3b10d159b8b6a9e2a4126592919a7d057bf38e5118b9a62
7
+ data.tar.gz: 3745c02ad5e63009a05279b610a96fcfecb79b42b789c617d609ef38aaa30e54a3941e040c650a26e0179353dcd7ef6b48dc5401a1ea69a77a31585a0eac0c40
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ## Mocha [![Build Status](https://travis-ci.org/freerange/mocha.svg?branch=master)](https://travis-ci.org/freerange/mocha) [![Gem Version](https://badge.fury.io/rb/mocha.png)](http://badge.fury.io/rb/mocha) [![OpenCollective](https://opencollective.com/mocha/backers/badge.svg)](#backers) [![OpenCollective](https://opencollective.com/mocha/sponsors/badge.svg)](#sponsors)
1
+ ## Mocha [![Build Status](https://travis-ci.org/freerange/mocha.svg?branch=master)](https://travis-ci.org/freerange/mocha) [![Gem Version](https://badge.fury.io/rb/mocha.svg)](http://badge.fury.io/rb/mocha) [![SemVer](https://api.dependabot.com/badges/compatibility_score?dependency-name=mocha&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=mocha&package-manager=bundler&version-scheme=semver)
2
2
 
3
3
 
4
4
  ### Description
@@ -266,6 +266,17 @@ When a method is invoked on a mock object, the mock object searches through its
266
266
 
267
267
  See the [documentation](http://gofreerange.com/mocha/docs/Mocha/Mock.html) for `Mocha::Mock` for further details.
268
268
 
269
+ ### Configuration
270
+
271
+ If you want, Mocha can generate a warning or raise an exception when:
272
+
273
+ * stubbing a method unnecessarily
274
+ * stubbing method on a non-mock object
275
+ * stubbing a non-existent method
276
+ * stubbing a non-public method
277
+
278
+ See the [documentation](http://gofreerange.com/mocha/docs/Mocha/Configuration.html) for `Mocha::Configuration` for further details.
279
+
269
280
  ### Useful Links
270
281
 
271
282
  * [Official Documentation](http://gofreerange.com/mocha/docs/)
@@ -282,76 +293,6 @@ See the [documentation](http://gofreerange.com/mocha/docs/Mocha/Mock.html) for `
282
293
 
283
294
  See this [list of contributors](https://github.com/freerange/mocha/graphs/contributors).
284
295
 
285
- ### Backers
286
-
287
- Support us with a monthly donation and help us continue our activities. [[Become a backer](https://opencollective.com/mocha#backer)]
288
-
289
- <a href="https://opencollective.com/mocha/backer/0/website" target="_blank"><img src="https://opencollective.com/mocha/backer/0/avatar.svg"></a>
290
- <a href="https://opencollective.com/mocha/backer/1/website" target="_blank"><img src="https://opencollective.com/mocha/backer/1/avatar.svg"></a>
291
- <a href="https://opencollective.com/mocha/backer/2/website" target="_blank"><img src="https://opencollective.com/mocha/backer/2/avatar.svg"></a>
292
- <a href="https://opencollective.com/mocha/backer/3/website" target="_blank"><img src="https://opencollective.com/mocha/backer/3/avatar.svg"></a>
293
- <a href="https://opencollective.com/mocha/backer/4/website" target="_blank"><img src="https://opencollective.com/mocha/backer/4/avatar.svg"></a>
294
- <a href="https://opencollective.com/mocha/backer/5/website" target="_blank"><img src="https://opencollective.com/mocha/backer/5/avatar.svg"></a>
295
- <a href="https://opencollective.com/mocha/backer/6/website" target="_blank"><img src="https://opencollective.com/mocha/backer/6/avatar.svg"></a>
296
- <a href="https://opencollective.com/mocha/backer/7/website" target="_blank"><img src="https://opencollective.com/mocha/backer/7/avatar.svg"></a>
297
- <a href="https://opencollective.com/mocha/backer/8/website" target="_blank"><img src="https://opencollective.com/mocha/backer/8/avatar.svg"></a>
298
- <a href="https://opencollective.com/mocha/backer/9/website" target="_blank"><img src="https://opencollective.com/mocha/backer/9/avatar.svg"></a>
299
- <a href="https://opencollective.com/mocha/backer/10/website" target="_blank"><img src="https://opencollective.com/mocha/backer/10/avatar.svg"></a>
300
- <a href="https://opencollective.com/mocha/backer/11/website" target="_blank"><img src="https://opencollective.com/mocha/backer/11/avatar.svg"></a>
301
- <a href="https://opencollective.com/mocha/backer/12/website" target="_blank"><img src="https://opencollective.com/mocha/backer/12/avatar.svg"></a>
302
- <a href="https://opencollective.com/mocha/backer/13/website" target="_blank"><img src="https://opencollective.com/mocha/backer/13/avatar.svg"></a>
303
- <a href="https://opencollective.com/mocha/backer/14/website" target="_blank"><img src="https://opencollective.com/mocha/backer/14/avatar.svg"></a>
304
- <a href="https://opencollective.com/mocha/backer/15/website" target="_blank"><img src="https://opencollective.com/mocha/backer/15/avatar.svg"></a>
305
- <a href="https://opencollective.com/mocha/backer/16/website" target="_blank"><img src="https://opencollective.com/mocha/backer/16/avatar.svg"></a>
306
- <a href="https://opencollective.com/mocha/backer/17/website" target="_blank"><img src="https://opencollective.com/mocha/backer/17/avatar.svg"></a>
307
- <a href="https://opencollective.com/mocha/backer/18/website" target="_blank"><img src="https://opencollective.com/mocha/backer/18/avatar.svg"></a>
308
- <a href="https://opencollective.com/mocha/backer/19/website" target="_blank"><img src="https://opencollective.com/mocha/backer/19/avatar.svg"></a>
309
- <a href="https://opencollective.com/mocha/backer/20/website" target="_blank"><img src="https://opencollective.com/mocha/backer/20/avatar.svg"></a>
310
- <a href="https://opencollective.com/mocha/backer/21/website" target="_blank"><img src="https://opencollective.com/mocha/backer/21/avatar.svg"></a>
311
- <a href="https://opencollective.com/mocha/backer/22/website" target="_blank"><img src="https://opencollective.com/mocha/backer/22/avatar.svg"></a>
312
- <a href="https://opencollective.com/mocha/backer/23/website" target="_blank"><img src="https://opencollective.com/mocha/backer/23/avatar.svg"></a>
313
- <a href="https://opencollective.com/mocha/backer/24/website" target="_blank"><img src="https://opencollective.com/mocha/backer/24/avatar.svg"></a>
314
- <a href="https://opencollective.com/mocha/backer/25/website" target="_blank"><img src="https://opencollective.com/mocha/backer/25/avatar.svg"></a>
315
- <a href="https://opencollective.com/mocha/backer/26/website" target="_blank"><img src="https://opencollective.com/mocha/backer/26/avatar.svg"></a>
316
- <a href="https://opencollective.com/mocha/backer/27/website" target="_blank"><img src="https://opencollective.com/mocha/backer/27/avatar.svg"></a>
317
- <a href="https://opencollective.com/mocha/backer/28/website" target="_blank"><img src="https://opencollective.com/mocha/backer/28/avatar.svg"></a>
318
- <a href="https://opencollective.com/mocha/backer/29/website" target="_blank"><img src="https://opencollective.com/mocha/backer/29/avatar.svg"></a>
319
-
320
- ### Sponsors
321
-
322
- Become a sponsor and get your logo on our README on Github with a link to your site. [[Become a sponsor](https://opencollective.com/mocha#sponsor)]
323
-
324
- <a href="https://opencollective.com/mocha/sponsor/0/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/0/avatar.svg"></a>
325
- <a href="https://opencollective.com/mocha/sponsor/1/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/1/avatar.svg"></a>
326
- <a href="https://opencollective.com/mocha/sponsor/2/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/2/avatar.svg"></a>
327
- <a href="https://opencollective.com/mocha/sponsor/3/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/3/avatar.svg"></a>
328
- <a href="https://opencollective.com/mocha/sponsor/4/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/4/avatar.svg"></a>
329
- <a href="https://opencollective.com/mocha/sponsor/5/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/5/avatar.svg"></a>
330
- <a href="https://opencollective.com/mocha/sponsor/6/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/6/avatar.svg"></a>
331
- <a href="https://opencollective.com/mocha/sponsor/7/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/7/avatar.svg"></a>
332
- <a href="https://opencollective.com/mocha/sponsor/8/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/8/avatar.svg"></a>
333
- <a href="https://opencollective.com/mocha/sponsor/9/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/9/avatar.svg"></a>
334
- <a href="https://opencollective.com/mocha/sponsor/10/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/10/avatar.svg"></a>
335
- <a href="https://opencollective.com/mocha/sponsor/11/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/11/avatar.svg"></a>
336
- <a href="https://opencollective.com/mocha/sponsor/12/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/12/avatar.svg"></a>
337
- <a href="https://opencollective.com/mocha/sponsor/13/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/13/avatar.svg"></a>
338
- <a href="https://opencollective.com/mocha/sponsor/14/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/14/avatar.svg"></a>
339
- <a href="https://opencollective.com/mocha/sponsor/15/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/15/avatar.svg"></a>
340
- <a href="https://opencollective.com/mocha/sponsor/16/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/16/avatar.svg"></a>
341
- <a href="https://opencollective.com/mocha/sponsor/17/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/17/avatar.svg"></a>
342
- <a href="https://opencollective.com/mocha/sponsor/18/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/18/avatar.svg"></a>
343
- <a href="https://opencollective.com/mocha/sponsor/19/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/19/avatar.svg"></a>
344
- <a href="https://opencollective.com/mocha/sponsor/20/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/20/avatar.svg"></a>
345
- <a href="https://opencollective.com/mocha/sponsor/21/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/21/avatar.svg"></a>
346
- <a href="https://opencollective.com/mocha/sponsor/22/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/22/avatar.svg"></a>
347
- <a href="https://opencollective.com/mocha/sponsor/23/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/23/avatar.svg"></a>
348
- <a href="https://opencollective.com/mocha/sponsor/24/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/24/avatar.svg"></a>
349
- <a href="https://opencollective.com/mocha/sponsor/25/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/25/avatar.svg"></a>
350
- <a href="https://opencollective.com/mocha/sponsor/26/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/26/avatar.svg"></a>
351
- <a href="https://opencollective.com/mocha/sponsor/27/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/27/avatar.svg"></a>
352
- <a href="https://opencollective.com/mocha/sponsor/28/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/28/avatar.svg"></a>
353
- <a href="https://opencollective.com/mocha/sponsor/29/website" target="_blank"><img src="https://opencollective.com/mocha/sponsor/29/avatar.svg"></a>
354
-
355
296
  ### Translations
356
297
 
357
298
  * [Serbo-Croatian](http://science.webhostinggeeks.com/mocha) by [WHG Team](http://webhostinggeeks.com/). (may be out-of-date)
data/RELEASE.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Release Notes
2
2
 
3
+ ## 1.6.0
4
+
5
+ * Fix subtle bug in setting correct visibility of stubbed module methods on `Kernel` or `Object` - thanks to @chrisroos (#295)
6
+ * Avoid mocks for partial mocking leaking into subsequent tests - thanks to @skliew for reporting (#331)
7
+ * Remove OpenCollective badge, backers & sponsors (a283a079)
8
+ * Change gem version badge to SVG format and add SemVer stability badge - thanks to @greysteil (#335)
9
+ * Improve documentation for Configuration (#236)
10
+
11
+ ## 1.5.0
12
+
13
+ * Prevent use of Mocha outside the context of a test/example - thanks to @andyw8 & @lzap (#327)
14
+
3
15
  ## 1.4.0
4
16
 
5
17
  * Fix deprecation warning for `assert_nil` in `ClassMethodTest` (#308 & #309)
data/lib/mocha/central.rb CHANGED
@@ -2,6 +2,23 @@ module Mocha
2
2
 
3
3
  class Central
4
4
 
5
+ class Null < self
6
+
7
+ def initialize(&block)
8
+ super
9
+ @raise_not_initialized_error = block
10
+ end
11
+
12
+ def stub(*)
13
+ @raise_not_initialized_error.call
14
+ end
15
+
16
+ def unstub(*)
17
+ @raise_not_initialized_error.call
18
+ end
19
+
20
+ end
21
+
5
22
  attr_accessor :stubba_methods
6
23
 
7
24
  def initialize
@@ -19,8 +19,12 @@ module Mocha
19
19
  @stubba_object = klass
20
20
  end
21
21
 
22
- def mocha
23
- @mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
22
+ def mocha(instantiate = true)
23
+ if instantiate
24
+ @mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
25
+ else
26
+ defined?(@mocha) ? @mocha : nil
27
+ end
24
28
  end
25
29
 
26
30
  def stubba_method
@@ -1,6 +1,71 @@
1
1
  module Mocha
2
2
 
3
- # Configuration settings.
3
+ # This class allows you to determine what should happen under certain circumstances. In each scenario, Mocha can be configured to {.allow do nothing}, {.warn_when display a warning message}, or {.prevent raise an exception}. The relevant scenario is identified using one of the following symbols:
4
+ #
5
+ # * +:stubbing_method_unnecessarily+ This is useful for identifying unused stubs. Unused stubs are often accidentally introduced when code is {http://martinfowler.com/bliki/DefinitionOfRefactoring.html refactored}. Allowed by default.
6
+ # * +:stubbing_non_existent_method+ - 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. Allowed by default.
7
+ # * +:stubbing_non_public_method+ - 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. Allowed by default.
8
+ # * +:stubbing_method_on_non_mock_object+ - 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. Allowed by default.
9
+ # * +:stubbing_method_on_nil+ - This is usually done accidentally, but there might be rare cases where it is intended. Prevented by default.
10
+ #
11
+ # @example Preventing unnecessary stubbing of a method
12
+ # Mocha::Configuration.prevent(:stubbing_method_unnecessarily)
13
+ #
14
+ # example = mock('example')
15
+ # example.stubs(:unused_stub)
16
+ # # => Mocha::StubbingError: stubbing method unnecessarily:
17
+ # # => #<Mock:example>.unused_stub(any_parameters)
18
+ #
19
+ # @example Preventing stubbing of a method on a non-mock object
20
+ # Mocha::Configuration.prevent(:stubbing_method_on_non_mock_object)
21
+ #
22
+ # class Example
23
+ # def example_method; end
24
+ # end
25
+ #
26
+ # example = Example.new
27
+ # example.stubs(:example_method)
28
+ # # => Mocha::StubbingError: stubbing method on non-mock object:
29
+ # # => #<Example:0x593620>.example_method
30
+ #
31
+ # @example Preventing stubbing of a non-existent method
32
+ #
33
+ # Mocha::Configuration.prevent(:stubbing_non_existent_method)
34
+ #
35
+ # class Example
36
+ # end
37
+ #
38
+ # example = Example.new
39
+ # example.stubs(:method_that_doesnt_exist)
40
+ # # => Mocha::StubbingError: stubbing non-existent method:
41
+ # # => #<Example:0x593760>.method_that_doesnt_exist
42
+ #
43
+ # @example Preventing stubbing of a non-public method
44
+ # Mocha::Configuration.prevent(:stubbing_non_public_method)
45
+ #
46
+ # class Example
47
+ # def internal_method; end
48
+ # private :internal_method
49
+ # end
50
+ #
51
+ # example = Example.new
52
+ # example.stubs(:internal_method)
53
+ # # => Mocha::StubbingError: stubbing non-public method:
54
+ # # => #<Example:0x593530>.internal_method
55
+ #
56
+ # Typically the configuration would be set globally in a +test_helper.rb+ or +spec_helper.rb+ file. However, it can also be temporarily overridden locally using the block syntax of the relevant method. In the latter case, the original configuration settings are restored when the block is exited.
57
+ #
58
+ # @example Temporarily allowing stubbing of a non-existent method
59
+ # Mocha::Configuration.prevent(:stubbing_non_public_method)
60
+ #
61
+ # class Example
62
+ # end
63
+ #
64
+ # Mocha::Configuration.allow(:stubbing_non_existent_method) do
65
+ # example = Example.new
66
+ # example.stubs(:method_that_doesnt_exist)
67
+ # # => no exception raised
68
+ # end
4
69
  class Configuration
5
70
 
6
71
  DEFAULTS = {
@@ -0,0 +1,17 @@
1
+ require 'mocha/backtrace_filter'
2
+
3
+ module Mocha
4
+
5
+ # @private
6
+ class ErrorWithFilteredBacktrace < StandardError
7
+
8
+ # @private
9
+ def initialize(message = nil, backtrace = [])
10
+ super(message)
11
+ filter = BacktraceFilter.new
12
+ set_backtrace(filter.filtered(backtrace))
13
+ end
14
+
15
+ end
16
+
17
+ end
data/lib/mocha/hooks.rb CHANGED
@@ -21,6 +21,7 @@ module Mocha
21
21
  #
22
22
  # This method should be called before each individual test starts (including before any "setup" code).
23
23
  def mocha_setup
24
+ Mockery.setup
24
25
  end
25
26
 
26
27
  # Verifies that all mock expectations have been met (only for use by authors of test libraries).
@@ -37,8 +38,6 @@ module Mocha
37
38
  # This method should be called after each individual test has finished (including after any "teardown" code).
38
39
  def mocha_teardown
39
40
  Mockery.teardown
40
- ensure
41
- Mockery.reset_instance
42
41
  end
43
42
  end
44
43
  end
@@ -25,6 +25,7 @@ module Mocha
25
25
  begin
26
26
  begin
27
27
  @passed = nil
28
+ mocha_setup
28
29
  self.setup
29
30
  self.__send__ self.name
30
31
  mocha_verify(assertion_counter)
@@ -25,6 +25,7 @@ module Mocha
25
25
  begin
26
26
  begin
27
27
  @passed = nil
28
+ mocha_setup
28
29
  self.setup
29
30
  self.__send__ self.__name__
30
31
  mocha_verify(assertion_counter)
@@ -31,6 +31,7 @@ module Mocha
31
31
  begin
32
32
  begin
33
33
  @passed = nil
34
+ mocha_setup
34
35
  self.setup
35
36
  self.__send__ self.__name__
36
37
  mocha_verify(assertion_counter)
@@ -31,6 +31,7 @@ module Mocha
31
31
  begin
32
32
  begin
33
33
  @passed = nil
34
+ mocha_setup
34
35
  self.setup
35
36
  self.__send__ self.__name__
36
37
  mocha_verify(assertion_counter)
@@ -31,6 +31,7 @@ module Mocha
31
31
  begin
32
32
  begin
33
33
  @passed = nil
34
+ mocha_setup
34
35
  self.setup
35
36
  self.__send__ self.__name__
36
37
  mocha_verify(assertion_counter)
@@ -31,6 +31,7 @@ module Mocha
31
31
  begin
32
32
  begin
33
33
  @passed = nil
34
+ mocha_setup
34
35
  self.setup
35
36
  self.__send__ self.__name__
36
37
  mocha_verify(assertion_counter)
@@ -32,6 +32,7 @@ module Mocha
32
32
  begin
33
33
  @passed = nil
34
34
  self.before_setup
35
+ mocha_setup
35
36
  self.setup
36
37
  self.after_setup
37
38
  self.run_test self.__name__
@@ -35,6 +35,7 @@ module Mocha
35
35
  begin
36
36
  @passed = nil
37
37
  self.before_setup
38
+ mocha_setup
38
39
  self.setup
39
40
  self.after_setup
40
41
  self.run_test self.__name__
@@ -31,6 +31,7 @@ module Mocha
31
31
  begin
32
32
  begin
33
33
  @passed = nil
34
+ mocha_setup
34
35
  self.setup
35
36
  self.run_setup_hooks
36
37
  self.__send__ self.__name__
@@ -26,6 +26,7 @@ module Mocha
26
26
  yield(Test::Unit::TestCase::STARTED, name)
27
27
  begin
28
28
  begin
29
+ mocha_setup
29
30
  run_setup
30
31
  __send__(@method_name)
31
32
  mocha_verify(assertion_counter)
@@ -26,6 +26,7 @@ module Mocha
26
26
  yield(Test::Unit::TestCase::STARTED, name)
27
27
  begin
28
28
  begin
29
+ mocha_setup
29
30
  run_setup
30
31
  run_test
31
32
  mocha_verify(assertion_counter)
@@ -26,6 +26,7 @@ module Mocha
26
26
  yield(Test::Unit::TestCase::STARTED, name)
27
27
  begin
28
28
  begin
29
+ mocha_setup
29
30
  run_setup
30
31
  run_test
31
32
  mocha_verify(assertion_counter)
@@ -28,6 +28,7 @@ module Mocha
28
28
  yield(Test::Unit::TestCase::STARTED_OBJECT, self)
29
29
  begin
30
30
  begin
31
+ mocha_setup
31
32
  run_setup
32
33
  run_test
33
34
  run_cleanup
@@ -25,6 +25,7 @@ module Mocha
25
25
  @_result = result
26
26
  begin
27
27
  begin
28
+ mocha_setup
28
29
  setup
29
30
  __send__(@method_name)
30
31
  mocha_verify(assertion_counter)
@@ -25,6 +25,7 @@ module Mocha
25
25
  @_result = result
26
26
  begin
27
27
  begin
28
+ mocha_setup
28
29
  setup
29
30
  __send__(@method_name)
30
31
  mocha_verify(assertion_counter)
data/lib/mocha/mockery.rb CHANGED
@@ -7,16 +7,46 @@ require 'mocha/state_machine'
7
7
  require 'mocha/logger'
8
8
  require 'mocha/configuration'
9
9
  require 'mocha/stubbing_error'
10
+ require 'mocha/not_initialized_error'
10
11
  require 'mocha/expectation_error_factory'
11
12
 
12
13
  module Mocha
13
14
 
14
15
  class Mockery
15
16
 
17
+ class Null < self
18
+
19
+ def add_mock(*)
20
+ raise_not_initialized_error
21
+ end
22
+
23
+ def add_state_machine(*)
24
+ raise_not_initialized_error
25
+ end
26
+
27
+ def stubba
28
+ Central::Null.new(&method(:raise_not_initialized_error))
29
+ end
30
+
31
+ private
32
+
33
+ def raise_not_initialized_error
34
+ message = 'Mocha methods cannot be used outside the context of a test'
35
+ raise NotInitializedError.new(message, caller)
36
+ end
37
+
38
+ end
39
+
16
40
  class << self
17
41
 
18
42
  def instance
19
- @instance ||= new
43
+ instances.last || Null.new
44
+ end
45
+
46
+ def setup
47
+ mockery = new
48
+ mockery.logger = instance.logger unless instances.empty?
49
+ @instances.push(mockery)
20
50
  end
21
51
 
22
52
  def verify(*args)
@@ -25,10 +55,15 @@ module Mocha
25
55
 
26
56
  def teardown
27
57
  instance.teardown
58
+ ensure
59
+ @instances.pop
60
+ @instances = nil if instances.empty?
28
61
  end
29
62
 
30
- def reset_instance
31
- @instance = nil
63
+ private
64
+
65
+ def instances
66
+ @instances ||= []
32
67
  end
33
68
 
34
69
  end
@@ -3,14 +3,6 @@ require 'mocha/class_method'
3
3
  module Mocha
4
4
 
5
5
  class ModuleMethod < ClassMethod
6
-
7
- def method_exists?(method)
8
- return true if stubbee.public_methods(false).include?(method)
9
- return true if stubbee.protected_methods(false).include?(method)
10
- return true if stubbee.private_methods(false).include?(method)
11
- return false
12
- end
13
-
14
6
  end
15
7
 
16
8
  end
@@ -0,0 +1,9 @@
1
+ require 'mocha/error_with_filtered_backtrace'
2
+
3
+ module Mocha
4
+
5
+ # Exception raised when Mocha has not been initialized, e.g. outside the
6
+ # context of a test.
7
+ class NotInitializedError < ErrorWithFilteredBacktrace; end
8
+
9
+ end
@@ -14,8 +14,12 @@ module Mocha
14
14
  alias_method :_method, :method
15
15
 
16
16
  # @private
17
- def mocha
18
- @mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
17
+ def mocha(instantiate = true)
18
+ if instantiate
19
+ @mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
20
+ else
21
+ defined?(@mocha) ? @mocha : nil
22
+ end
19
23
  end
20
24
 
21
25
  # @private
@@ -9,7 +9,8 @@ module Mocha
9
9
  def mocks
10
10
  object, mocks = @object, []
11
11
  while object do
12
- mocks << object.mocha
12
+ mocha = object.mocha(false)
13
+ mocks << mocha if mocha
13
14
  object = object.is_a?(Class) ? object.superclass : nil
14
15
  end
15
16
  mocks
@@ -26,7 +27,8 @@ module Mocha
26
27
  def mocks
27
28
  klass, mocks = @klass, []
28
29
  while klass do
29
- mocks << klass.any_instance.mocha
30
+ mocha = klass.any_instance.mocha(false)
31
+ mocks << mocha if mocha
30
32
  klass = klass.superclass
31
33
  end
32
34
  mocks
@@ -1,19 +1,10 @@
1
- require 'mocha/backtrace_filter'
1
+ require 'mocha/error_with_filtered_backtrace'
2
2
 
3
3
  module Mocha
4
4
 
5
5
  # Exception raised when stubbing a particular method is not allowed.
6
6
  #
7
7
  # @see Configuration.prevent
8
- class StubbingError < StandardError
9
-
10
- # @private
11
- def initialize(message = nil, backtrace = [])
12
- super(message)
13
- filter = BacktraceFilter.new
14
- set_backtrace(filter.filtered(backtrace))
15
- end
16
-
17
- end
8
+ class StubbingError < ErrorWithFilteredBacktrace; end
18
9
 
19
10
  end
data/lib/mocha/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Mocha
2
- VERSION = "1.4.0"
2
+ VERSION = "1.6.0"
3
3
  end
@@ -27,13 +27,11 @@ module AcceptanceTest
27
27
  Mocha::Configuration.reset_configuration
28
28
  @logger = FakeLogger.new
29
29
  mockery = Mocha::Mockery.instance
30
- @original_logger = mockery.logger
31
30
  mockery.logger = @logger
32
31
  end
33
32
 
34
33
  def teardown_acceptance_test
35
34
  Mocha::Configuration.reset_configuration
36
- Mocha::Mockery.instance.logger = @original_logger
37
35
  end
38
36
 
39
37
  include Introspection::Assertions
@@ -0,0 +1,79 @@
1
+ require File.expand_path('../acceptance_test_helper', __FILE__)
2
+ require 'mocha/setup'
3
+ require 'mocha/not_initialized_error'
4
+
5
+ class PreventUseOfMochaOutsideTestTest < Mocha::TestCase
6
+
7
+ include AcceptanceTest
8
+
9
+ def setup
10
+ setup_acceptance_test
11
+ mocha_teardown
12
+ end
13
+
14
+ def teardown
15
+ teardown_acceptance_test
16
+ end
17
+
18
+ def test_should_raise_exception_when_mock_called_outside_test
19
+ assert_raises(Mocha::NotInitializedError) { mock('object') }
20
+ end
21
+
22
+ def test_should_raise_exception_when_stub_called_outside_test
23
+ assert_raises(Mocha::NotInitializedError) { stub('object') }
24
+ end
25
+
26
+ def test_should_raise_exception_when_stub_everything_called_outside_test
27
+ assert_raises(Mocha::NotInitializedError) { stub_everything('object') }
28
+ end
29
+
30
+ def test_should_raise_exception_when_states_called_outside_test
31
+ assert_raises(Mocha::NotInitializedError) { states('state-machine') }
32
+ end
33
+
34
+ def test_should_raise_exception_when_expects_called_on_instance_outside_test
35
+ instance = Class.new.new
36
+ assert_raises(Mocha::NotInitializedError) { instance.expects(:expected_method) }
37
+ end
38
+
39
+ def test_should_raise_exception_when_expects_called_on_class_outside_test
40
+ klass = Class.new
41
+ assert_raises(Mocha::NotInitializedError) { klass.expects(:expected_method) }
42
+ end
43
+
44
+ def test_should_raise_exception_when_expects_called_on_any_instance_outside_test
45
+ klass = Class.new
46
+ assert_raises(Mocha::NotInitializedError) { klass.any_instance.expects(:expected_method) }
47
+ end
48
+
49
+ def test_should_raise_exception_when_stubs_called_on_instance_outside_test
50
+ instance = Class.new.new
51
+ assert_raises(Mocha::NotInitializedError) { instance.stubs(:expected_method) }
52
+ end
53
+
54
+ def test_should_raise_exception_when_stubs_called_on_class_outside_test
55
+ klass = Class.new
56
+ assert_raises(Mocha::NotInitializedError) { klass.stubs(:expected_method) }
57
+ end
58
+
59
+ def test_should_raise_exception_when_stubs_called_on_any_instance_outside_test
60
+ klass = Class.new
61
+ assert_raises(Mocha::NotInitializedError) { klass.any_instance.stubs(:expected_method) }
62
+ end
63
+
64
+ def test_should_raise_exception_when_unstub_called_on_instance_outside_test
65
+ instance = Class.new.new
66
+ assert_raises(Mocha::NotInitializedError) { instance.unstub(:expected_method) }
67
+ end
68
+
69
+ def test_should_raise_exception_when_unstub_called_on_class_outside_test
70
+ klass = Class.new
71
+ assert_raises(Mocha::NotInitializedError) { klass.unstub(:expected_method) }
72
+ end
73
+
74
+ def test_should_raise_exception_when_unstub_called_on_any_instance_outside_test
75
+ klass = Class.new
76
+ assert_raises(Mocha::NotInitializedError) { klass.any_instance.unstub(:expected_method) }
77
+ end
78
+
79
+ end
@@ -31,4 +31,34 @@ class StubAnyInstanceMethodDefinedOnSuperclassTest < Mocha::TestCase
31
31
  end
32
32
  assert_equal :original_return_value, instance.my_superclass_method
33
33
  end
34
- end
34
+
35
+ def test_expect_method_on_any_instance_of_superclass_even_if_preceded_by_test_expecting_method_on_any_instance_of_subclass
36
+ superklass = Class.new do
37
+ def self.inspect
38
+ 'superklass'
39
+ end
40
+ def my_instance_method; end
41
+ end
42
+ klass = Class.new(superklass) do
43
+ def self.inspect
44
+ 'klass'
45
+ end
46
+ def my_instance_method; end
47
+ end
48
+ test_result = run_as_tests(
49
+ :test_1 => lambda {
50
+ klass.any_instance.expects(:my_instance_method)
51
+ klass.new.my_instance_method
52
+ },
53
+ :test_2 => lambda {
54
+ superklass.any_instance.expects(:my_instance_method)
55
+ }
56
+ )
57
+ assert_failed(test_result)
58
+ assert_equal [
59
+ "not all expectations were satisfied",
60
+ "unsatisfied expectations:",
61
+ "- expected exactly once, not yet invoked: #<AnyInstance:superklass>.my_instance_method(any_parameters)"
62
+ ], test_result.failure_message_lines
63
+ end
64
+ end
@@ -109,4 +109,34 @@ class StubClassMethodDefinedOnSuperclassTest < Mocha::TestCase
109
109
  end
110
110
  assert_passed(test_result)
111
111
  end
112
+
113
+ def test_expect_method_on_superclass_even_if_preceded_by_test_expecting_method_on_subclass
114
+ superklass = Class.new do
115
+ def self.inspect
116
+ 'superklass'
117
+ end
118
+ def self.my_class_method; end
119
+ end
120
+ klass = Class.new(superklass) do
121
+ def self.inspect
122
+ 'klass'
123
+ end
124
+ def self.my_class_method; end
125
+ end
126
+ test_result = run_as_tests(
127
+ :test_1 => lambda {
128
+ klass.expects(:my_class_method)
129
+ klass.my_class_method
130
+ },
131
+ :test_2 => lambda {
132
+ superklass.expects(:my_class_method)
133
+ }
134
+ )
135
+ assert_failed(test_result)
136
+ assert_equal [
137
+ "not all expectations were satisfied",
138
+ "unsatisfied expectations:",
139
+ "- expected exactly once, not yet invoked: superklass.my_class_method(any_parameters)"
140
+ ], test_result.failure_message_lines
141
+ end
112
142
  end
@@ -72,4 +72,67 @@ class StubInstanceMethodDefinedOnKernelModuleTest < Mocha::TestCase
72
72
  ensure
73
73
  Kernel.module_eval { remove_method :my_instance_method }
74
74
  end
75
+
76
+ def test_should_stub_public_module_method_and_leave_it_unchanged_after_test
77
+ Kernel.module_eval do
78
+ def my_instance_method
79
+ :original_return_value
80
+ end
81
+ public :my_instance_method
82
+ end
83
+ mod = Module.new
84
+ assert_snapshot_unchanged(mod) do
85
+ test_result = run_as_test do
86
+ mod.stubs(:my_instance_method).returns(:new_return_value)
87
+ assert_method_visibility mod, :my_instance_method, :public
88
+ assert_equal :new_return_value, mod.my_instance_method
89
+ end
90
+ assert_passed(test_result)
91
+ end
92
+ assert_equal :original_return_value, mod.my_instance_method
93
+ ensure
94
+ Kernel.module_eval { remove_method :my_instance_method }
95
+ end
96
+
97
+ def test_should_stub_protected_module_method_and_leave_it_unchanged_after_test
98
+ Kernel.module_eval do
99
+ def my_instance_method
100
+ :original_return_value
101
+ end
102
+ protected :my_instance_method
103
+ end
104
+ mod = Module.new
105
+ assert_snapshot_unchanged(mod) do
106
+ test_result = run_as_test do
107
+ mod.stubs(:my_instance_method).returns(:new_return_value)
108
+ assert_method_visibility mod, :my_instance_method, :protected
109
+ assert_equal :new_return_value, mod.send(:my_instance_method)
110
+ end
111
+ assert_passed(test_result)
112
+ end
113
+ assert_equal :original_return_value, mod.send(:my_instance_method)
114
+ ensure
115
+ Kernel.module_eval { remove_method :my_instance_method }
116
+ end
117
+
118
+ def test_should_stub_private_module_method_and_leave_it_unchanged_after_test
119
+ Kernel.module_eval do
120
+ def my_instance_method
121
+ :original_return_value
122
+ end
123
+ private :my_instance_method
124
+ end
125
+ mod = Module.new
126
+ assert_snapshot_unchanged(mod) do
127
+ test_result = run_as_test do
128
+ mod.stubs(:my_instance_method).returns(:new_return_value)
129
+ assert_method_visibility mod, :my_instance_method, :private
130
+ assert_equal :new_return_value, mod.send(:my_instance_method)
131
+ end
132
+ assert_passed(test_result)
133
+ end
134
+ assert_equal :original_return_value, mod.send(:my_instance_method)
135
+ ensure
136
+ Kernel.module_eval { remove_method :my_instance_method }
137
+ end
75
138
  end
@@ -1,13 +1,20 @@
1
1
  require File.expand_path('../../test_helper', __FILE__)
2
2
  require 'mocha/class_methods'
3
3
  require 'mocha/object_methods'
4
+ require 'mocha/mockery'
5
+ require 'mocha/names'
4
6
 
5
7
  class ClassMethodsTest < Mocha::TestCase
6
8
 
7
9
  def setup
10
+ Mocha::Mockery.setup
8
11
  @klass = Class.new.extend(Mocha::ClassMethods, Mocha::ObjectMethods)
9
12
  end
10
13
 
14
+ def teardown
15
+ Mocha::Mockery.teardown
16
+ end
17
+
11
18
  def test_should_build_any_instance_object
12
19
  any_instance = @klass.any_instance
13
20
  assert_not_nil any_instance
@@ -20,6 +27,30 @@ class ClassMethodsTest < Mocha::TestCase
20
27
  assert_equal any_instance_1, any_instance_2
21
28
  end
22
29
 
30
+ def test_any_instance_should_build_mocha_referring_to_klass
31
+ mocha = @klass.any_instance.mocha
32
+ assert_not_nil mocha
33
+ assert mocha.is_a?(Mocha::Mock)
34
+ expected_name = Mocha::ImpersonatingAnyInstanceName.new(@klass).mocha_inspect
35
+ assert_equal expected_name, mocha.mocha_inspect
36
+ end
37
+
38
+ def test_any_instance_should_not_build_mocha_if_instantiate_is_false
39
+ assert_nil @klass.any_instance.mocha(false)
40
+ end
41
+
42
+ def test_any_instance_should_reuse_existing_mocha
43
+ mocha_1 = @klass.any_instance.mocha
44
+ mocha_2 = @klass.any_instance.mocha
45
+ assert_equal mocha_1, mocha_2
46
+ end
47
+
48
+ def test_any_instance_should_reuse_existing_mocha_even_if_instantiate_is_false
49
+ mocha_1 = @klass.any_instance.mocha
50
+ mocha_2 = @klass.any_instance.mocha(false)
51
+ assert_equal mocha_1, mocha_2
52
+ end
53
+
23
54
  def test_should_use_stubba_class_method_for_class
24
55
  assert_equal Mocha::ClassMethod, @klass.stubba_method
25
56
  end
@@ -1,10 +1,11 @@
1
1
  require File.expand_path('../../test_helper', __FILE__)
2
2
  require 'mocha/hooks'
3
+ require 'mocha/mockery'
3
4
 
4
5
  class HooksTest < Mocha::TestCase
5
6
  class Mocha::Mockery
6
7
  class << self
7
- attr_writer :instance
8
+ attr_writer :instances
8
9
  end
9
10
  end
10
11
 
@@ -19,11 +20,11 @@ class HooksTest < Mocha::TestCase
19
20
 
20
21
  def test_ensure_mockery_instance_is_reset_even_when_an_exception_is_raised_in_mockery_teardown
21
22
  fake_test_case = Object.new.extend(Mocha::Hooks)
22
- original_mockery = FakeMockery.new
23
- Mocha::Mockery.instance = original_mockery
23
+ mockery = FakeMockery.new
24
+ Mocha::Mockery.instances = [mockery]
24
25
 
25
26
  fake_test_case.mocha_teardown rescue nil
26
27
 
27
- assert_not_same Mocha::Mockery.instance, original_mockery
28
+ assert_kind_of Mocha::Mockery::Null, Mocha::Mockery.instance
28
29
  end
29
30
  end
@@ -9,7 +9,22 @@ class MockeryTest < Mocha::TestCase
9
9
  include Mocha
10
10
  include DeprecationDisabler
11
11
 
12
- def test_should_build_instance_of_mockery
12
+ def setup
13
+ Mockery.setup
14
+ end
15
+
16
+ def teardown
17
+ Mockery.teardown
18
+ end
19
+
20
+ def test_should_return_null_mockery_if_not_setup
21
+ Mockery.teardown
22
+ mockery = Mockery.instance
23
+ assert_not_nil mockery
24
+ assert_kind_of Mockery::Null, mockery
25
+ end
26
+
27
+ def test_should_return_instance_of_mockery
13
28
  mockery = Mockery.instance
14
29
  assert_not_nil mockery
15
30
  assert_kind_of Mockery, mockery
@@ -23,7 +38,7 @@ class MockeryTest < Mocha::TestCase
23
38
 
24
39
  def test_should_expire_mockery_instance_cache
25
40
  mockery_1 = Mockery.instance
26
- Mockery.reset_instance
41
+ Mockery.teardown
27
42
  mockery_2 = Mockery.instance
28
43
  assert_not_same mockery_1, mockery_2
29
44
  end
@@ -1,19 +1,31 @@
1
1
  require File.expand_path('../../test_helper', __FILE__)
2
2
  require 'mocha/object_methods'
3
+ require 'mocha/mockery'
3
4
  require 'mocha/mock'
4
5
  require 'mocha/expectation_error_factory'
6
+ require 'mocha/names'
5
7
 
6
8
  class ObjectMethodsTest < Mocha::TestCase
7
9
 
8
10
  def setup
11
+ Mocha::Mockery.setup
9
12
  @object = Object.new.extend(Mocha::ObjectMethods)
10
13
  end
11
14
 
15
+ def teardown
16
+ Mocha::Mockery.teardown
17
+ end
18
+
12
19
  def test_should_build_mocha_referring_to_self
13
20
  mocha = @object.mocha
14
21
  assert_not_nil mocha
15
22
  assert mocha.is_a?(Mocha::Mock)
16
- assert_equal @object.mocha_inspect, mocha.mocha_inspect
23
+ expected_name = Mocha::ImpersonatingName.new(@object).mocha_inspect
24
+ assert_equal expected_name, mocha.mocha_inspect
25
+ end
26
+
27
+ def test_should_not_build_mocha_if_instantiate_is_false
28
+ assert_nil @object.mocha(false)
17
29
  end
18
30
 
19
31
  def test_should_reuse_existing_mocha
@@ -22,6 +34,12 @@ class ObjectMethodsTest < Mocha::TestCase
22
34
  assert_equal mocha_1, mocha_2
23
35
  end
24
36
 
37
+ def test_should_reuse_existing_mocha_even_if_instantiate_is_false
38
+ mocha_1 = @object.mocha
39
+ mocha_2 = @object.mocha(false)
40
+ assert_equal mocha_1, mocha_2
41
+ end
42
+
25
43
  def test_should_reset_mocha
26
44
  assert_nil @object.reset_mocha
27
45
  end
@@ -4,13 +4,31 @@ require 'mocha/receivers'
4
4
  class ObjectReceiverTest < Mocha::TestCase
5
5
  include Mocha
6
6
 
7
- class FakeObject < Struct.new(:mocha)
7
+ class FakeObject
8
+ def initialize(mocha)
9
+ @mocha = mocha
10
+ end
11
+
12
+ def mocha(_)
13
+ @mocha
14
+ end
15
+
8
16
  def is_a?(klass)
9
17
  false
10
18
  end
11
19
  end
12
20
 
13
- class FakeClass < Struct.new(:superclass, :mocha)
21
+ class FakeClass
22
+ attr_reader :superclass
23
+
24
+ def initialize(superclass, mocha)
25
+ @superclass, @mocha = superclass, mocha
26
+ end
27
+
28
+ def mocha(_)
29
+ @mocha
30
+ end
31
+
14
32
  def is_a?(klass)
15
33
  klass == Class
16
34
  end
@@ -35,6 +53,16 @@ class AnyInstanceReceiverTest < Mocha::TestCase
35
53
  include Mocha
36
54
 
37
55
  class FakeAnyInstanceClass
56
+ class AnyInstance
57
+ def initialize(mocha)
58
+ @mocha = mocha
59
+ end
60
+
61
+ def mocha(_)
62
+ @mocha
63
+ end
64
+ end
65
+
38
66
  attr_reader :superclass
39
67
 
40
68
  def initialize(superclass, mocha)
@@ -42,7 +70,7 @@ class AnyInstanceReceiverTest < Mocha::TestCase
42
70
  end
43
71
 
44
72
  def any_instance
45
- Struct.new(:mocha).new(@mocha)
73
+ AnyInstance.new(@mocha)
46
74
  end
47
75
  end
48
76
 
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: 1.4.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Mead
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-20 00:00:00.000000000 Z
11
+ date: 2018-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: metaclass
@@ -141,6 +141,7 @@ files:
141
141
  - lib/mocha/deprecation.rb
142
142
  - lib/mocha/detection/mini_test.rb
143
143
  - lib/mocha/detection/test_unit.rb
144
+ - lib/mocha/error_with_filtered_backtrace.rb
144
145
  - lib/mocha/exception_raiser.rb
145
146
  - lib/mocha/expectation.rb
146
147
  - lib/mocha/expectation_error.rb
@@ -187,6 +188,7 @@ files:
187
188
  - lib/mocha/multiple_yields.rb
188
189
  - lib/mocha/names.rb
189
190
  - lib/mocha/no_yields.rb
191
+ - lib/mocha/not_initialized_error.rb
190
192
  - lib/mocha/object_methods.rb
191
193
  - lib/mocha/parameter_matchers.rb
192
194
  - lib/mocha/parameter_matchers/all_of.rb
@@ -250,6 +252,7 @@ files:
250
252
  - test/acceptance/parameter_matcher_test.rb
251
253
  - test/acceptance/partial_mocks_test.rb
252
254
  - test/acceptance/prepend_test.rb
255
+ - test/acceptance/prevent_use_of_mocha_outside_test_test.rb
253
256
  - test/acceptance/raise_exception_test.rb
254
257
  - test/acceptance/return_value_test.rb
255
258
  - test/acceptance/sequence_test.rb