mocha 0.10.1 → 0.10.2
Sign up to get free protection for your applications and to get access to all the features.
- data/RELEASE.rdoc +5 -2
- data/lib/mocha/any_instance_method.rb +1 -7
- data/lib/mocha/class_method.rb +1 -1
- data/lib/mocha/expectation_list.rb +4 -0
- data/lib/mocha/mock.rb +4 -0
- data/lib/mocha/version.rb +1 -1
- data/test/acceptance/stub_any_instance_method_test.rb +2 -1
- data/test/acceptance/unstubbing_test.rb +30 -1
- data/test/unit/any_instance_method_test.rb +12 -8
- data/test/unit/class_method_test.rb +9 -8
- data/test/unit/expectation_list_test.rb +14 -0
- data/test/unit/mock_test.rb +10 -3
- metadata +4 -4
data/RELEASE.rdoc
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
-
= 0.10.
|
2
|
-
* Merge pull request #
|
1
|
+
= 0.10.2 ()
|
2
|
+
* Merge pull request #53. Unstubbing a method should not remove expectations for other stubbed methods. Fixes #52. Thanks to saikat.
|
3
|
+
|
4
|
+
= 0.10.1 (f631a4ba22c6ed4929c52b0520311a9a84034a20)
|
5
|
+
* Merge pull request #51. Use Gem::Requirement & Gem::Version for version comparison. Fixes issue #50. Thanks to meineerde.
|
3
6
|
* Fixed typo in rdoc for Mocha::ObjectMethods.
|
4
7
|
* Improve README as suggested in issue #46. Explain that Mocha must be loaded after test libraries and how to achieve this using Bundler.
|
5
8
|
* Merge pull request #43 - nobody expects the spanish inquisition! Thanks to cairo140.
|
@@ -4,12 +4,6 @@ module Mocha
|
|
4
4
|
|
5
5
|
class AnyInstanceMethod < ClassMethod
|
6
6
|
|
7
|
-
def unstub
|
8
|
-
remove_new_method
|
9
|
-
restore_original_method
|
10
|
-
stubbee.any_instance.reset_mocha
|
11
|
-
end
|
12
|
-
|
13
7
|
def mock
|
14
8
|
stubbee.any_instance.mocha
|
15
9
|
end
|
@@ -56,4 +50,4 @@ module Mocha
|
|
56
50
|
|
57
51
|
end
|
58
52
|
|
59
|
-
end
|
53
|
+
end
|
data/lib/mocha/class_method.rb
CHANGED
@@ -10,6 +10,10 @@ module Mocha # :nodoc:
|
|
10
10
|
@expectations.unshift(expectation)
|
11
11
|
expectation
|
12
12
|
end
|
13
|
+
|
14
|
+
def remove_all_matching_method(method_name)
|
15
|
+
@expectations.reject! { |expectation| expectation.matches_method?(method_name) }
|
16
|
+
end
|
13
17
|
|
14
18
|
def matches_method?(method_name)
|
15
19
|
@expectations.any? { |expectation| expectation.matches_method?(method_name) }
|
data/lib/mocha/mock.rb
CHANGED
@@ -82,6 +82,10 @@ module Mocha # :nodoc:
|
|
82
82
|
}
|
83
83
|
end
|
84
84
|
|
85
|
+
def unstub(method_name)
|
86
|
+
@expectations.remove_all_matching_method(method_name)
|
87
|
+
end
|
88
|
+
|
85
89
|
# :call-seq: responds_like(responder) -> mock
|
86
90
|
#
|
87
91
|
# Constrains the +mock+ so that it can only expect or stub methods to which +responder+ responds. The constraint is only applied at method invocation time.
|
data/lib/mocha/version.rb
CHANGED
@@ -84,6 +84,7 @@ class StubAnyInstanceMethodTest < Test::Unit::TestCase
|
|
84
84
|
run_as_test do
|
85
85
|
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value)
|
86
86
|
end
|
87
|
+
|
87
88
|
assert_equal 0, klass.any_instance.mocha.expectations.length
|
88
89
|
end
|
89
90
|
|
@@ -195,4 +196,4 @@ class StubAnyInstanceMethodTest < Test::Unit::TestCase
|
|
195
196
|
assert_passed(test_result)
|
196
197
|
end
|
197
198
|
|
198
|
-
end
|
199
|
+
end
|
@@ -119,4 +119,33 @@ class UnstubbingTest < Test::Unit::TestCase
|
|
119
119
|
assert_passed(test_result)
|
120
120
|
end
|
121
121
|
|
122
|
-
|
122
|
+
def test_unstubbing_a_method_should_not_unstub_other_stubbed_methods
|
123
|
+
klass = Class.new do
|
124
|
+
def my_first_instance_method; :first_return_value; end
|
125
|
+
def my_second_instance_method; :second_return_value; end
|
126
|
+
end
|
127
|
+
|
128
|
+
test_result = run_as_test do
|
129
|
+
object = klass.new
|
130
|
+
object.stubs(:my_first_instance_method).returns(:first_new_return_value)
|
131
|
+
object.stubs(:my_second_instance_method).returns(:second_new_return_value)
|
132
|
+
object.unstub(:my_first_instance_method)
|
133
|
+
assert_equal :first_return_value, object.my_first_instance_method
|
134
|
+
assert_equal :second_new_return_value, object.my_second_instance_method
|
135
|
+
end
|
136
|
+
assert_passed(test_result)
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_unstubbing_a_method_should_remove_all_expectations_for_that_method
|
140
|
+
klass = Class.new do
|
141
|
+
def my_instance_method; :original_return_value; end
|
142
|
+
end
|
143
|
+
test_result = run_as_test do
|
144
|
+
object = klass.new
|
145
|
+
object.expects(:my_instance_method).with(:first)
|
146
|
+
object.expects(:my_instance_method).with(:second)
|
147
|
+
object.unstub(:my_instance_method)
|
148
|
+
end
|
149
|
+
assert_passed(test_result)
|
150
|
+
end
|
151
|
+
end
|
@@ -73,7 +73,8 @@ class AnyInstanceMethodTest < Test::Unit::TestCase
|
|
73
73
|
def test_should_call_remove_new_method
|
74
74
|
klass = Class.new { def method_x; end }
|
75
75
|
any_instance = Mock.new
|
76
|
-
|
76
|
+
any_instance_mocha = Mock.new
|
77
|
+
any_instance.stubs(:mocha).returns(any_instance_mocha)
|
77
78
|
klass.define_instance_method(:any_instance) { any_instance }
|
78
79
|
method = AnyInstanceMethod.new(klass, :method_x)
|
79
80
|
method.replace_instance_method(:restore_original_method) { }
|
@@ -88,7 +89,8 @@ class AnyInstanceMethodTest < Test::Unit::TestCase
|
|
88
89
|
def test_should_call_restore_original_method
|
89
90
|
klass = Class.new { def method_x; end }
|
90
91
|
any_instance = Mock.new
|
91
|
-
|
92
|
+
any_instance_mocha = Mock.new
|
93
|
+
any_instance.stubs(:mocha).returns(any_instance_mocha)
|
92
94
|
klass.define_instance_method(:any_instance) { any_instance }
|
93
95
|
method = AnyInstanceMethod.new(klass, :method_x)
|
94
96
|
method.replace_instance_method(:remove_new_method) { }
|
@@ -100,17 +102,19 @@ class AnyInstanceMethodTest < Test::Unit::TestCase
|
|
100
102
|
assert method.restore_called
|
101
103
|
end
|
102
104
|
|
103
|
-
def
|
105
|
+
def test_should_call_mock_unstub
|
104
106
|
klass = Class.new { def method_x; end }
|
105
|
-
|
106
|
-
klass.define_instance_method(:any_instance) { any_instance }
|
107
|
+
|
107
108
|
method = AnyInstanceMethod.new(klass, :method_x)
|
109
|
+
|
108
110
|
method.replace_instance_method(:remove_new_method) { }
|
109
111
|
method.replace_instance_method(:restore_original_method) { }
|
110
|
-
|
112
|
+
mocha = Class.new { class << self; attr_accessor :unstub_method; end; def self.unstub(method); self.unstub_method = method; end; }
|
113
|
+
method.replace_instance_method(:mock) { mocha }
|
114
|
+
|
111
115
|
method.unstub
|
112
116
|
|
113
|
-
|
117
|
+
assert_equal mocha.unstub_method, :method_x
|
114
118
|
end
|
115
119
|
|
116
120
|
def test_should_return_any_instance_mocha_for_stubbee
|
@@ -123,4 +127,4 @@ class AnyInstanceMethodTest < Test::Unit::TestCase
|
|
123
127
|
assert_equal stubbee.any_instance.mocha, method.mock
|
124
128
|
end
|
125
129
|
|
126
|
-
end
|
130
|
+
end
|
@@ -154,8 +154,9 @@ class ClassMethodTest < Test::Unit::TestCase
|
|
154
154
|
|
155
155
|
def test_should_call_remove_new_method
|
156
156
|
klass = Class.new { def self.method_x; end }
|
157
|
-
klass.define_instance_method(:reset_mocha) { }
|
158
157
|
method = ClassMethod.new(klass, :method_x)
|
158
|
+
mocha = Mock.new
|
159
|
+
klass.define_instance_method(:mocha) { mocha }
|
159
160
|
method.define_instance_accessor(:remove_called)
|
160
161
|
method.replace_instance_method(:remove_new_method) { self.remove_called = true }
|
161
162
|
|
@@ -166,7 +167,8 @@ class ClassMethodTest < Test::Unit::TestCase
|
|
166
167
|
|
167
168
|
def test_should_call_restore_original_method
|
168
169
|
klass = Class.new { def self.method_x; end }
|
169
|
-
|
170
|
+
mocha = Mock.new
|
171
|
+
klass.define_instance_method(:mocha) { mocha }
|
170
172
|
method = ClassMethod.new(klass, :method_x)
|
171
173
|
method.define_instance_accessor(:restore_called)
|
172
174
|
method.replace_instance_method(:restore_original_method) { self.restore_called = true }
|
@@ -176,16 +178,15 @@ class ClassMethodTest < Test::Unit::TestCase
|
|
176
178
|
assert method.restore_called
|
177
179
|
end
|
178
180
|
|
179
|
-
def
|
181
|
+
def test_should_call_mocha_unstub
|
180
182
|
klass = Class.new { def self.method_x; end }
|
181
|
-
klass.define_instance_accessor(:reset_called)
|
182
|
-
klass.define_instance_method(:reset_mocha) { self.reset_called = true }
|
183
183
|
method = ClassMethod.new(klass, :method_x)
|
184
184
|
method.replace_instance_method(:restore_original_method) { }
|
185
|
+
mocha = Class.new { class << self; attr_accessor :unstub_method; end; def self.unstub(method); self.unstub_method = method; end; }
|
186
|
+
method.replace_instance_method(:mock) { mocha }
|
185
187
|
|
186
188
|
method.unstub
|
187
|
-
|
188
|
-
assert klass.reset_called
|
189
|
+
assert_equal mocha.unstub_method, :method_x
|
189
190
|
end
|
190
191
|
|
191
192
|
def test_should_return_mock_for_stubbee
|
@@ -234,4 +235,4 @@ class ClassMethodTest < Test::Unit::TestCase
|
|
234
235
|
assert class_method_1.matches?(class_method_2)
|
235
236
|
end
|
236
237
|
|
237
|
-
end
|
238
|
+
end
|
@@ -22,6 +22,20 @@ class ExpectationListTest < Test::Unit::TestCase
|
|
22
22
|
expectation_list.add(expectation2)
|
23
23
|
assert_same expectation1, expectation_list.match(:my_method, :argument1, :argument2)
|
24
24
|
end
|
25
|
+
|
26
|
+
def test_should_remove_all_expectations_matching_method_name
|
27
|
+
expectation_list = ExpectationList.new
|
28
|
+
expectation1 = Expectation.new(nil, :method_one).with(:argument1, :argument2)
|
29
|
+
expectation2 = Expectation.new(nil, :method_one).with(:argument3, :argument4)
|
30
|
+
expectation3 = Expectation.new(nil, :method_two)
|
31
|
+
expectation_list.add(expectation1)
|
32
|
+
expectation_list.add(expectation2)
|
33
|
+
expectation_list.add(expectation3)
|
34
|
+
expectation_list.remove_all_matching_method(:method_one)
|
35
|
+
assert_nil expectation_list.match(:method_one, :argument1, :argument2)
|
36
|
+
assert_nil expectation_list.match(:method_one, :argument3, :argument4)
|
37
|
+
assert_same expectation3, expectation_list.match(:method_two)
|
38
|
+
end
|
25
39
|
|
26
40
|
def test_should_find_most_recent_matching_expectation
|
27
41
|
expectation_list = ExpectationList.new
|
data/test/unit/mock_test.rb
CHANGED
@@ -27,7 +27,7 @@ class MockTest < Test::Unit::TestCase
|
|
27
27
|
mock = Mock.new
|
28
28
|
assert_equal false, mock.everything_stubbed
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
def test_should_stub_everything
|
32
32
|
mock = Mock.new
|
33
33
|
mock.stub_everything
|
@@ -298,5 +298,12 @@ class MockTest < Test::Unit::TestCase
|
|
298
298
|
assert mock.respond_to?(:abc)
|
299
299
|
assert mock.respond_to?(:xyz)
|
300
300
|
end
|
301
|
-
|
302
|
-
|
301
|
+
|
302
|
+
def test_should_remove_expectation_for_unstubbed_method
|
303
|
+
mock = Mock.new
|
304
|
+
mock.expects(:method1)
|
305
|
+
mock.unstub(:method1)
|
306
|
+
e = assert_raises(ExpectationError) { mock.method1 }
|
307
|
+
assert_match(/unexpected invocation/, e.message)
|
308
|
+
end
|
309
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mocha
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 51
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 10
|
9
|
-
-
|
10
|
-
version: 0.10.
|
9
|
+
- 2
|
10
|
+
version: 0.10.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- James Mead
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-01-
|
18
|
+
date: 2012-01-17 00:00:00 +00:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|