mocha 1.5.0 → 1.6.0

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
  SHA1:
3
- metadata.gz: ae952b50093776beef6c7083184899e6875738d7
4
- data.tar.gz: b45fc4c6fe7d7ab128a7227f3fdf2db01e349989
3
+ metadata.gz: c4c3e82b675148ae87d3cb16bd97816bc04b04b3
4
+ data.tar.gz: 888491a9247d93053283fbfa0b7a59d073beb760
5
5
  SHA512:
6
- metadata.gz: c5ca31997f0e6342f4d757a4a42da220cbc51878b878b48f61b4d74b095f7347a846c6196f65568c4dde737c8ccbe13e15eba3fce25054d5232724535021a1b3
7
- data.tar.gz: b8797e8b7e3a58b31e3f773860989aafc85fabd2cbf4529a965b521e7ec1f995304b26186f49062c6ec8af6b8b4749730e89373ebf1e909df1b7e7fb5ca793a1
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,13 @@
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
+
3
11
  ## 1.5.0
4
12
 
5
13
  * Prevent use of Mocha outside the context of a test/example - thanks to @andyw8 & @lzap (#327)
@@ -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 = {
@@ -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
@@ -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,3 +1,3 @@
1
1
  module Mocha
2
- VERSION = "1.5.0"
2
+ VERSION = "1.6.0"
3
3
  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
@@ -3,6 +3,7 @@ require 'mocha/object_methods'
3
3
  require 'mocha/mockery'
4
4
  require 'mocha/mock'
5
5
  require 'mocha/expectation_error_factory'
6
+ require 'mocha/names'
6
7
 
7
8
  class ObjectMethodsTest < Mocha::TestCase
8
9
 
@@ -19,7 +20,12 @@ class ObjectMethodsTest < Mocha::TestCase
19
20
  mocha = @object.mocha
20
21
  assert_not_nil mocha
21
22
  assert mocha.is_a?(Mocha::Mock)
22
- 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)
23
29
  end
24
30
 
25
31
  def test_should_reuse_existing_mocha
@@ -28,6 +34,12 @@ class ObjectMethodsTest < Mocha::TestCase
28
34
  assert_equal mocha_1, mocha_2
29
35
  end
30
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
+
31
43
  def test_should_reset_mocha
32
44
  assert_nil @object.reset_mocha
33
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.5.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-04-06 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