mocha 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CONTRIBUTING.md +12 -0
- data/README.md +0 -12
- data/RELEASE.md +8 -0
- data/lib/mocha/any_instance_method.rb +24 -2
- data/lib/mocha/class_method.rb +24 -3
- data/lib/mocha/mock.rb +4 -0
- data/lib/mocha/object_methods.rb +2 -2
- data/lib/mocha/version.rb +1 -1
- data/test/acceptance/prepend_test.rb +88 -0
- data/test/acceptance/stub_any_instance_method_test.rb +36 -1
- data/test/assertions.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fee7267881fbeec218036679d2d7002f5a2f0f30
|
4
|
+
data.tar.gz: 8801ccb9858bbafe1a422702ca1d91e19d31c777
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1a6ca8a2301842c8ecba0db5df9223232af8076e3d4745763307264c635f68ccb2ff13e8b359dad1d03950341f4fd226cbbda3ef6e22d3bcdea140b776a449e
|
7
|
+
data.tar.gz: 2cc8b79a37943f4039764780c9c602d30d63e083e6bf375937e387ef6e3f08369f8669dc0b55e99216f15aa6672f9a6033dc52a91e08a172f55b0c59d7f7bc6f
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
* Pull requests are welcomed.
|
2
|
+
* Fork the repository.
|
3
|
+
* Make your changes in a branch.
|
4
|
+
* Add tests for new behaviour. Modify/remove existing tests for changes to existing behaviour.
|
5
|
+
* Run `bin/build-matrix` from the root directory and ensure all the tests pass.
|
6
|
+
* This script depends on `rbenv` being installed.
|
7
|
+
* You must have all the ruby versions listed in `.travis.yml` under the `rvm` key installed (currently 1.8.7, 1.9.3 & 2.0.0).
|
8
|
+
* I use `rbenv-aliases` to alias the patch versions.
|
9
|
+
* Note that the build matrix takes quite a while to run.
|
10
|
+
* Send us a pull request from your fork/branch.
|
11
|
+
* Wait for your pull request to build on [Travis CI](https://travis-ci.org/freerange/mocha).
|
12
|
+
* I will not accept pull requests with failing tests.
|
data/README.md
CHANGED
@@ -258,18 +258,6 @@ See the [documentation](http://gofreerange.com/mocha/docs/Mocha/Mock.html) for `
|
|
258
258
|
* [Mock Roles Not Objects](http://www.jmock.org/oopsla2004.pdf)
|
259
259
|
* [jMock](http://www.jmock.org/)
|
260
260
|
|
261
|
-
### Contributing
|
262
|
-
|
263
|
-
* Fork the repository.
|
264
|
-
* Make your changes in a branch.
|
265
|
-
* Add tests for new behaviour. Modify existing tests for changes to existing behaviour.
|
266
|
-
* Run `bin/build-matrix` from the root directory and ensure all the tests pass.
|
267
|
-
* This script depends on `rbenv` being installed.
|
268
|
-
* You must have all the ruby versions listed in `.travis.yml` under the `rvm` key installed (currently 1.8.7, 1.9.3 & 2.0.0).
|
269
|
-
* I use `rbenv-aliases` to alias the patch versions.
|
270
|
-
* Note that the build matrix takes quite a while to run.
|
271
|
-
* Send us a pull request from your fork/branch.
|
272
|
-
|
273
261
|
### Contributors
|
274
262
|
|
275
263
|
See this [list of contributors](https://github.com/freerange/mocha/graphs/contributors).
|
data/RELEASE.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# Release Notes
|
2
2
|
|
3
|
+
## 1.1.0
|
4
|
+
|
5
|
+
* Set visibility of any instance stub method.
|
6
|
+
* Stub methods with a prepended method if there are other prepended methods. Thanks to @mrsimo.
|
7
|
+
* Improve docs for `Mock#responds_like` & `#responds_like_instance_of`.
|
8
|
+
* Use GitHub convention for instructions on contributing to Mocha.
|
9
|
+
* Fix typos in docs. Thanks to @10io
|
10
|
+
|
3
11
|
## 1.0.0
|
4
12
|
|
5
13
|
### External changes
|
@@ -25,6 +25,8 @@ module Mocha
|
|
25
25
|
end
|
26
26
|
stubbee.send(:remove_method, method)
|
27
27
|
end
|
28
|
+
|
29
|
+
include_prepended_module if RUBY_VERSION >= '2.0'
|
28
30
|
rescue NameError
|
29
31
|
# deal with nasties like ActiveRecord::Associations::AssociationProxy
|
30
32
|
end
|
@@ -32,15 +34,18 @@ module Mocha
|
|
32
34
|
end
|
33
35
|
|
34
36
|
def define_new_method
|
35
|
-
|
37
|
+
definition_target.class_eval(<<-CODE, __FILE__, __LINE__ + 1)
|
36
38
|
def #{method}(*args, &block)
|
37
39
|
self.class.any_instance.mocha.method_missing(:#{method}, *args, &block)
|
38
40
|
end
|
39
41
|
CODE
|
42
|
+
if @original_visibility
|
43
|
+
Module.instance_method(@original_visibility).bind(definition_target).call(method)
|
44
|
+
end
|
40
45
|
end
|
41
46
|
|
42
47
|
def remove_new_method
|
43
|
-
|
48
|
+
definition_target.send(:remove_method, method)
|
44
49
|
end
|
45
50
|
|
46
51
|
def restore_original_method
|
@@ -57,6 +62,23 @@ module Mocha
|
|
57
62
|
return false
|
58
63
|
end
|
59
64
|
|
65
|
+
private
|
66
|
+
|
67
|
+
def include_prepended_module
|
68
|
+
possible_prepended_modules = stubbee.ancestors.take_while do |mod|
|
69
|
+
!(Class === mod)
|
70
|
+
end
|
71
|
+
|
72
|
+
if possible_prepended_modules.any?
|
73
|
+
@definition_target = PrependedModule.new
|
74
|
+
stubbee.__send__ :prepend, @definition_target
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def definition_target
|
79
|
+
@definition_target ||= stubbee
|
80
|
+
end
|
81
|
+
|
60
82
|
end
|
61
83
|
|
62
84
|
end
|
data/lib/mocha/class_method.rb
CHANGED
@@ -4,6 +4,8 @@ module Mocha
|
|
4
4
|
|
5
5
|
class ClassMethod
|
6
6
|
|
7
|
+
PrependedModule = Class.new(Module)
|
8
|
+
|
7
9
|
attr_reader :stubbee, :method
|
8
10
|
|
9
11
|
def initialize(stubbee, method)
|
@@ -47,6 +49,8 @@ module Mocha
|
|
47
49
|
if @original_method && @original_method.owner == stubbee.__metaclass__
|
48
50
|
stubbee.__metaclass__.send(:remove_method, method)
|
49
51
|
end
|
52
|
+
|
53
|
+
include_prepended_module if RUBY_VERSION >= '2.0'
|
50
54
|
rescue NameError
|
51
55
|
# deal with nasties like ActiveRecord::Associations::AssociationProxy
|
52
56
|
end
|
@@ -54,18 +58,18 @@ module Mocha
|
|
54
58
|
end
|
55
59
|
|
56
60
|
def define_new_method
|
57
|
-
|
61
|
+
definition_target.class_eval(<<-CODE, __FILE__, __LINE__ + 1)
|
58
62
|
def #{method}(*args, &block)
|
59
63
|
mocha.method_missing(:#{method}, *args, &block)
|
60
64
|
end
|
61
65
|
CODE
|
62
66
|
if @original_visibility
|
63
|
-
Module.instance_method(@original_visibility).bind(
|
67
|
+
Module.instance_method(@original_visibility).bind(definition_target).call(method)
|
64
68
|
end
|
65
69
|
end
|
66
70
|
|
67
71
|
def remove_new_method
|
68
|
-
|
72
|
+
definition_target.send(:remove_method, method)
|
69
73
|
end
|
70
74
|
|
71
75
|
def restore_original_method
|
@@ -101,6 +105,23 @@ module Mocha
|
|
101
105
|
__metaclass__.public_method_defined?(symbol) || __metaclass__.protected_method_defined?(symbol) || __metaclass__.private_method_defined?(symbol)
|
102
106
|
end
|
103
107
|
|
108
|
+
private
|
109
|
+
|
110
|
+
def include_prepended_module
|
111
|
+
possible_prepended_modules = stubbee.__metaclass__.ancestors.take_while do |mod|
|
112
|
+
!(Class === mod)
|
113
|
+
end
|
114
|
+
|
115
|
+
if possible_prepended_modules.any?
|
116
|
+
@definition_target = PrependedModule.new
|
117
|
+
stubbee.__metaclass__.__send__ :prepend, @definition_target
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def definition_target
|
122
|
+
@definition_target ||= stubbee.__metaclass__
|
123
|
+
end
|
124
|
+
|
104
125
|
end
|
105
126
|
|
106
127
|
end
|
data/lib/mocha/mock.rb
CHANGED
@@ -170,6 +170,8 @@ module Mocha
|
|
170
170
|
#
|
171
171
|
# The {Mock} instance will delegate its +#respond_to?+ method to the +responder+.
|
172
172
|
#
|
173
|
+
# Note that the methods on +responder+ are never actually invoked.
|
174
|
+
#
|
173
175
|
# @param [Object, #respond_to?] responder an object used to determine whether {Mock} instance should +#respond_to?+ to an invocation.
|
174
176
|
# @return [Mock] the same {Mock} instance, thereby allowing invocations of other {Mock} methods to be chained.
|
175
177
|
# @see #responds_like_instance_of
|
@@ -222,6 +224,8 @@ module Mocha
|
|
222
224
|
#
|
223
225
|
# The {Mock} instance will delegate its +#respond_to?+ method to the responder instance.
|
224
226
|
#
|
227
|
+
# Note that the methods on the responder instance are never actually invoked.
|
228
|
+
#
|
225
229
|
# @param [Class] responder_class a class used to determine whether {Mock} instance should +#respond_to?+ to an invocation.
|
226
230
|
# @return [Mock] the same {Mock} instance, thereby allowing invocations of other {Mock} methods to be chained.
|
227
231
|
# @see #responds_like
|
data/lib/mocha/object_methods.rb
CHANGED
@@ -35,7 +35,7 @@ module Mocha
|
|
35
35
|
|
36
36
|
# Adds an expectation that the specified method must be called exactly once with any parameters.
|
37
37
|
#
|
38
|
-
# The original implementation of the method is replaced during the test and then restored at the end of the test. The temporary replacement method has the same
|
38
|
+
# The original implementation of the method is replaced during the test and then restored at the end of the test. The temporary replacement method has the same visibility as the original method.
|
39
39
|
#
|
40
40
|
# @param [Symbol,String] method_name name of expected method
|
41
41
|
# @param [Hash] expected_methods_vs_return_values expected method name symbols as keys and corresponding return values as values - these expectations are setup as if {#expects} were called multiple times.
|
@@ -84,7 +84,7 @@ module Mocha
|
|
84
84
|
|
85
85
|
# Adds an expectation that the specified method may be called any number of times with any parameters.
|
86
86
|
#
|
87
|
-
# The original implementation of the method is replaced during the test and then restored at the end of the test. The temporary replacement method has the same
|
87
|
+
# The original implementation of the method is replaced during the test and then restored at the end of the test. The temporary replacement method has the same visibility as the original method.
|
88
88
|
#
|
89
89
|
# @param [Symbol,String] method_name name of stubbed method
|
90
90
|
# @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {#stubs} were called multiple times.
|
data/lib/mocha/version.rb
CHANGED
@@ -0,0 +1,88 @@
|
|
1
|
+
require File.expand_path('../acceptance_test_helper', __FILE__)
|
2
|
+
require 'mocha/setup'
|
3
|
+
|
4
|
+
class PrependTest < Mocha::TestCase
|
5
|
+
|
6
|
+
include AcceptanceTest
|
7
|
+
|
8
|
+
def setup
|
9
|
+
setup_acceptance_test
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
teardown_acceptance_test
|
14
|
+
end
|
15
|
+
|
16
|
+
if RUBY_VERSION >= '2.0'
|
17
|
+
|
18
|
+
module Mod1
|
19
|
+
def my_method
|
20
|
+
super + " World"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module Mod2
|
25
|
+
def my_method
|
26
|
+
super + " Wide"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class Klass1
|
31
|
+
prepend Mod1
|
32
|
+
prepend Mod2
|
33
|
+
|
34
|
+
def my_method
|
35
|
+
"Hello"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class Klass2
|
40
|
+
class << self
|
41
|
+
prepend Mod1
|
42
|
+
prepend Mod2
|
43
|
+
|
44
|
+
def my_method
|
45
|
+
"Hello"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_stubbing_any_instance_with_multiple_prepended_methods
|
51
|
+
assert_snapshot_unchanged(Klass1) do
|
52
|
+
test_result = run_as_test do
|
53
|
+
Klass1.any_instance.stubs(:my_method).returns("Bye World")
|
54
|
+
assert_equal "Bye World", Klass1.new.my_method
|
55
|
+
end
|
56
|
+
assert_passed(test_result)
|
57
|
+
end
|
58
|
+
assert_equal "Hello World Wide", Klass1.new.my_method
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_stubbing_instance_with_multiple_prepended_methods
|
62
|
+
object = Klass1.new
|
63
|
+
|
64
|
+
assert_snapshot_unchanged(object) do
|
65
|
+
test_result = run_as_test do
|
66
|
+
object.stubs(:my_method).returns("Bye World")
|
67
|
+
assert_equal "Bye World", object.my_method
|
68
|
+
assert_equal "Hello World Wide", Klass1.new.my_method
|
69
|
+
end
|
70
|
+
assert_passed(test_result)
|
71
|
+
end
|
72
|
+
assert_equal "Hello World Wide", object.my_method
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_stubbing_a_prepended_class_method
|
76
|
+
assert_snapshot_unchanged(Klass2) do
|
77
|
+
test_result = run_as_test do
|
78
|
+
Klass2.stubs(:my_method).returns("Bye World")
|
79
|
+
assert_equal "Bye World", Klass2.my_method
|
80
|
+
end
|
81
|
+
assert_passed(test_result)
|
82
|
+
end
|
83
|
+
assert_equal "Hello World Wide", Klass2.my_method
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
@@ -13,7 +13,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
13
13
|
teardown_acceptance_test
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
16
|
+
def test_should_stub_public_method_within_test
|
17
17
|
klass = Class.new do
|
18
18
|
def my_instance_method
|
19
19
|
:original_return_value
|
@@ -63,6 +63,25 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
63
63
|
assert_equal :original_return_value, instance.my_unprotected_instance_method
|
64
64
|
end
|
65
65
|
|
66
|
+
def test_should_stub_protected_method_within_test
|
67
|
+
klass = Class.new do
|
68
|
+
def my_instance_method
|
69
|
+
:original_return_value
|
70
|
+
end
|
71
|
+
protected :my_instance_method
|
72
|
+
def self.protected(*args); end
|
73
|
+
def my_unprotected_instance_method
|
74
|
+
my_instance_method
|
75
|
+
end
|
76
|
+
end
|
77
|
+
instance = klass.new
|
78
|
+
test_result = run_as_test do
|
79
|
+
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value)
|
80
|
+
assert_method_visiblity instance, :my_instance_method, :protected
|
81
|
+
end
|
82
|
+
assert_passed(test_result)
|
83
|
+
end
|
84
|
+
|
66
85
|
def test_should_leave_stubbed_private_method_unchanged_after_test
|
67
86
|
klass = Class.new do
|
68
87
|
def my_instance_method
|
@@ -79,6 +98,22 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
79
98
|
assert_equal :original_return_value, instance.send(:my_instance_method)
|
80
99
|
end
|
81
100
|
|
101
|
+
def test_should_stub_private_method_within_test
|
102
|
+
klass = Class.new do
|
103
|
+
def my_instance_method
|
104
|
+
:original_return_value
|
105
|
+
end
|
106
|
+
private :my_instance_method
|
107
|
+
def self.private(*args); end
|
108
|
+
end
|
109
|
+
instance = klass.new
|
110
|
+
test_result = run_as_test do
|
111
|
+
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value)
|
112
|
+
assert_method_visiblity instance, :my_instance_method, :private
|
113
|
+
end
|
114
|
+
assert_passed(test_result)
|
115
|
+
end
|
116
|
+
|
82
117
|
def test_should_reset_expectations_after_test
|
83
118
|
klass = Class.new do
|
84
119
|
def my_instance_method
|
data/test/assertions.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Assertions
|
2
2
|
def assert_method_visiblity(object, method_name, visiblity)
|
3
3
|
method_key = RUBY_VERSION < '1.9' ? method_name.to_s : method_name.to_sym
|
4
|
-
assert object.send("#{visiblity}_methods", false).include?(method_key)
|
4
|
+
assert object.send("#{visiblity}_methods", false).include?(method_key), "#{method_name} is not #{visiblity}"
|
5
5
|
end
|
6
6
|
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: 1.
|
4
|
+
version: 1.1.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: 2014-
|
11
|
+
date: 2014-05-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: metaclass
|
@@ -89,6 +89,7 @@ extra_rdoc_files: []
|
|
89
89
|
files:
|
90
90
|
- .gemtest
|
91
91
|
- .yardopts
|
92
|
+
- CONTRIBUTING.md
|
92
93
|
- COPYING.md
|
93
94
|
- Gemfile
|
94
95
|
- MIT-LICENSE.md
|
@@ -231,6 +232,7 @@ files:
|
|
231
232
|
- test/acceptance/optional_parameters_test.rb
|
232
233
|
- test/acceptance/parameter_matcher_test.rb
|
233
234
|
- test/acceptance/partial_mocks_test.rb
|
235
|
+
- test/acceptance/prepend_test.rb
|
234
236
|
- test/acceptance/raise_exception_test.rb
|
235
237
|
- test/acceptance/return_value_test.rb
|
236
238
|
- test/acceptance/sequence_test.rb
|
@@ -355,7 +357,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
355
357
|
version: '0'
|
356
358
|
requirements: []
|
357
359
|
rubyforge_project: mocha
|
358
|
-
rubygems_version: 2.
|
360
|
+
rubygems_version: 2.0.14
|
359
361
|
signing_key:
|
360
362
|
specification_version: 3
|
361
363
|
summary: Mocking and stubbing library
|