mocha 0.9.9 → 0.9.10
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.
- data/README.rdoc +3 -3
- data/RELEASE.rdoc +6 -0
- data/Rakefile +3 -3
- data/lib/mocha/central.rb +9 -3
- data/lib/mocha/expectation_error.rb +1 -1
- data/lib/mocha/integration/mini_test/version_142_and_above.rb +1 -1
- data/lib/mocha/mock.rb +1 -0
- data/lib/mocha/object.rb +33 -0
- data/test/acceptance/exception_rescue_test.rb +58 -0
- data/test/acceptance/unstubbing_test.rb +122 -0
- data/test/unit/central_test.rb +26 -0
- metadata +6 -4
data/README.rdoc
CHANGED
@@ -26,9 +26,9 @@ Or download Mocha...
|
|
26
26
|
|
27
27
|
== Examples
|
28
28
|
|
29
|
-
* Quick Start - {Usage Examples}[
|
30
|
-
* Traditional mocking - {Star Trek Example}[
|
31
|
-
* Setting expectations on real classes - {Order Example}[
|
29
|
+
* Quick Start - {Usage Examples}[http://mocha.rubyforge.org/examples/misc.html]
|
30
|
+
* Traditional mocking - {Star Trek Example}[http://mocha.rubyforge.org/examples/mocha.html]
|
31
|
+
* Setting expectations on real classes - {Order Example}[http://mocha.rubyforge.org/examples/stubba.html]
|
32
32
|
* More examples on {Floehopper's Blog}[http://blog.floehopper.org]
|
33
33
|
* {Mailing List Archives}[http://groups.google.com/group/mocha-developer]
|
34
34
|
|
data/RELEASE.rdoc
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
= 0.9.10 ()
|
2
|
+
* Added Mocha::ObjectMethods#unstub method - https://github.com/floehopper/mocha/issues#issue/6
|
3
|
+
* Inherit Mocha::ExpectationError from Exception instead of StandardError to reduce the chances of a test passing by accident - thanks to James Sanders (jsanders) - https://github.com/floehopper/mocha/issues#issue/15
|
4
|
+
* Fixed bug - GitHub README page to link correctly to code examples - https://github.com/floehopper/mocha/issues/closed#issue/11
|
5
|
+
* Fixed bug - PASSTHROUGH_EXCEPTIONS are defined on MiniTest::Unit::TestCase not in Mocha - thanks to Brian Troutwine (blt) - https://github.com/floehopper/mocha/issues/closed#issue/14
|
6
|
+
|
1
7
|
= 0.9.9 (ee3a79db4d52c3339e8acf07505e01236a2b4810)
|
2
8
|
* Avoid loading bits of the test-unit gem by accident. This is an attempt at a fix for the problem that James Adam reported [1]. By using 'load' instead of 'require' to detect the version of Test::Unit, we can avoid rubygems trying to load bits of the test-unit gem when it's not wanted. [1] http://floehopper.lighthouseapp.com/projects/22289-mocha/tickets/50#ticket-50-13
|
3
9
|
* Fix exception when running rake without test-unit gem. When test-unit gem >=v2.0.0 was installed but the "use_test_unit_gem" MOCHA_OPTIONS was not specified, a "comparison of Fixnum with Hash failed" exception was being raised when running the performance tests. This was because bits of the test-unit gem were being loaded accidentally and a Hash was being incorrectly supplied to the TestRunner.run method.
|
data/Rakefile
CHANGED
@@ -3,7 +3,7 @@ require 'rake/gempackagetask'
|
|
3
3
|
require 'rake/testtask'
|
4
4
|
|
5
5
|
module Mocha
|
6
|
-
VERSION = "0.9.
|
6
|
+
VERSION = "0.9.10"
|
7
7
|
end
|
8
8
|
|
9
9
|
desc "Run all tests"
|
@@ -174,7 +174,7 @@ Rake::GemPackageTask.new(specification) do |package|
|
|
174
174
|
end
|
175
175
|
|
176
176
|
desc 'Generate updated gemspec with unique version, which will cause gem to be auto-built on github.'
|
177
|
-
task :
|
177
|
+
task :gemspec do
|
178
178
|
File.open('mocha.gemspec', 'w') do |output|
|
179
179
|
output << build_specification(Mocha::VERSION + '.' + Time.now.strftime('%Y%m%d%H%M%S')).to_ruby
|
180
180
|
end
|
@@ -209,7 +209,7 @@ task 'publish_packages' => ['verify_user', 'verify_password', 'clobber_package',
|
|
209
209
|
end
|
210
210
|
|
211
211
|
desc "Do a full release."
|
212
|
-
task 'release' => ['default', 'generate_docs', 'publish_packages', 'publish_docs', '
|
212
|
+
task 'release' => ['default', 'generate_docs', 'publish_packages', 'publish_docs', 'gemspec'] do
|
213
213
|
puts
|
214
214
|
puts "*** Remember to commit newly generated gemspec after release ***"
|
215
215
|
puts
|
data/lib/mocha/central.rb
CHANGED
@@ -15,10 +15,16 @@ module Mocha
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
def
|
19
|
-
|
20
|
-
method = stubba_methods.pop
|
18
|
+
def unstub(method)
|
19
|
+
if stubba_methods.include?(method)
|
21
20
|
method.unstub
|
21
|
+
stubba_methods.delete(method)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def unstub_all
|
26
|
+
while stubba_methods.any? do
|
27
|
+
unstub(stubba_methods.first)
|
22
28
|
end
|
23
29
|
end
|
24
30
|
|
@@ -35,7 +35,7 @@ module Mocha
|
|
35
35
|
ensure
|
36
36
|
begin
|
37
37
|
self.teardown
|
38
|
-
rescue
|
38
|
+
rescue *::MiniTest::Unit::TestCase::PASSTHROUGH_EXCEPTIONS
|
39
39
|
raise
|
40
40
|
rescue Exception => e
|
41
41
|
result = runner.puke(self.class, self.__name__, Mocha::Integration::MiniTest.translate(e))
|
data/lib/mocha/mock.rb
CHANGED
data/lib/mocha/object.rb
CHANGED
@@ -101,6 +101,39 @@ module Mocha
|
|
101
101
|
}
|
102
102
|
expectation
|
103
103
|
end
|
104
|
+
|
105
|
+
# :call-seq: unstub(*method_names)
|
106
|
+
#
|
107
|
+
# Removes the method stub added by calls to #expects or #stubs.
|
108
|
+
# Restores the original behaviour of the method before it was stubbed.
|
109
|
+
# multiplier = Multiplier.new
|
110
|
+
# multiplier.double(2) # => 4
|
111
|
+
# multiplier.stubs(:double).raises
|
112
|
+
# multiplier.double(2) # => raises exception
|
113
|
+
# multiplier.unstubs(:double)
|
114
|
+
# multiplier.double(2) # => 4
|
115
|
+
#
|
116
|
+
# The original implementation of <tt>Multiplier#double</tt> is replaced temporarily.
|
117
|
+
#
|
118
|
+
# The original implementation of <tt>Multiplier#double</tt> is restored when #unstub is called.
|
119
|
+
#
|
120
|
+
# WARNING: If you #unstub a method which still has unsatisfied expectations, you may be removing
|
121
|
+
# the only way those expectations can be satisfied. Use #unstub with care.
|
122
|
+
#
|
123
|
+
# If multiple +method_names+ are supplied, each method is unstubbed.
|
124
|
+
# multiplier.unstub(:double, :triple)
|
125
|
+
#
|
126
|
+
# # exactly equivalent to
|
127
|
+
#
|
128
|
+
# multiplier.unstub(:double)
|
129
|
+
# multiplier.unstub(:triple)
|
130
|
+
def unstub(*method_names)
|
131
|
+
mockery = Mocha::Mockery.instance
|
132
|
+
method_names.each do |method_name|
|
133
|
+
method = stubba_method.new(stubba_object, method_name)
|
134
|
+
mockery.stubba.unstub(method)
|
135
|
+
end
|
136
|
+
end
|
104
137
|
|
105
138
|
def method_exists?(method, include_public_methods = true) # :nodoc:
|
106
139
|
if include_public_methods
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require File.expand_path('../acceptance_test_helper', __FILE__)
|
2
|
+
require 'mocha'
|
3
|
+
|
4
|
+
class ExceptionRescueTest < Test::Unit::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
|
+
def test_unexpected_invocation_exception_is_not_caught_by_standard_rescue
|
17
|
+
test_result = run_as_test do
|
18
|
+
mock = mock('mock')
|
19
|
+
begin
|
20
|
+
mock.some_method
|
21
|
+
rescue => e
|
22
|
+
flunk "should not rescue #{e.class}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
assert_failed(test_result)
|
26
|
+
failure_message_lines = test_result.failure_messages.first.split("\n")
|
27
|
+
assert_equal "unexpected invocation: #<Mock:mock>.some_method()", failure_message_lines.first
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_invocation_never_expected_exception_is_not_caught_by_standard_rescue
|
31
|
+
test_result = run_as_test do
|
32
|
+
mock = mock('mock')
|
33
|
+
mock.expects(:some_method).never
|
34
|
+
begin
|
35
|
+
mock.some_method
|
36
|
+
rescue => e
|
37
|
+
flunk "should not rescue #{e.class}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
assert_failed(test_result)
|
41
|
+
failure_message_lines = test_result.failure_messages.first.split("\n")
|
42
|
+
assert_equal "unexpected invocation: #<Mock:mock>.some_method()", failure_message_lines.first
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_unsatisfied_expectation_exception_is_not_caught_by_standard_rescue
|
46
|
+
test_result = run_as_test do
|
47
|
+
mock = mock('mock')
|
48
|
+
mock.expects(:some_method)
|
49
|
+
end
|
50
|
+
assert_failed(test_result)
|
51
|
+
failure_message_lines = test_result.failure_messages.first.split("\n")
|
52
|
+
assert_equal [
|
53
|
+
"not all expectations were satisfied",
|
54
|
+
"unsatisfied expectations:",
|
55
|
+
"- expected exactly once, not yet invoked: #<Mock:mock>.some_method(any_parameters)"
|
56
|
+
], failure_message_lines
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require File.expand_path('../acceptance_test_helper', __FILE__)
|
2
|
+
require 'mocha'
|
3
|
+
|
4
|
+
class UnstubbingTest < Test::Unit::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
|
+
def test_unstubbing_an_instance_method_should_restore_original_behaviour
|
17
|
+
klass = Class.new do
|
18
|
+
def my_instance_method; :original_return_value; end
|
19
|
+
end
|
20
|
+
test_result = run_as_test do
|
21
|
+
object = klass.new
|
22
|
+
object.stubs(:my_instance_method).returns(:new_return_value)
|
23
|
+
object.unstub(:my_instance_method)
|
24
|
+
assert_equal :original_return_value, object.my_instance_method
|
25
|
+
end
|
26
|
+
assert_passed(test_result)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_unstubbing_a_class_method_should_restore_original_behaviour
|
30
|
+
klass = Class.new do
|
31
|
+
def self.my_class_method; :original_return_value; end
|
32
|
+
end
|
33
|
+
test_result = run_as_test do
|
34
|
+
klass.stubs(:my_class_method).returns(:new_return_value)
|
35
|
+
klass.unstub(:my_class_method)
|
36
|
+
assert_equal :original_return_value, klass.my_class_method
|
37
|
+
end
|
38
|
+
assert_passed(test_result)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_unstubbing_a_module_method_should_restore_original_behaviour
|
42
|
+
mod = Module.new do
|
43
|
+
def self.my_module_method; :original_return_value; end
|
44
|
+
end
|
45
|
+
test_result = run_as_test do
|
46
|
+
mod.stubs(:my_module_method).returns(:new_return_value)
|
47
|
+
mod.unstub(:my_module_method)
|
48
|
+
assert_equal :original_return_value, mod.my_module_method
|
49
|
+
end
|
50
|
+
assert_passed(test_result)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_unstubbing_an_any_instance_method_should_restore_original_behaviour
|
54
|
+
klass = Class.new do
|
55
|
+
def my_instance_method; :original_return_value; end
|
56
|
+
end
|
57
|
+
test_result = run_as_test do
|
58
|
+
object = klass.new
|
59
|
+
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value)
|
60
|
+
klass.any_instance.unstub(:my_instance_method)
|
61
|
+
assert_equal :original_return_value, object.my_instance_method
|
62
|
+
end
|
63
|
+
assert_passed(test_result)
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_unstubbing_multiple_methods_should_restore_original_behaviour
|
67
|
+
klass = Class.new do
|
68
|
+
def my_first_instance_method; :original_return_value; end
|
69
|
+
def my_second_instance_method; :original_return_value; end
|
70
|
+
end
|
71
|
+
test_result = run_as_test do
|
72
|
+
object = klass.new
|
73
|
+
object.stubs(:my_first_instance_method).returns(:new_return_value)
|
74
|
+
object.stubs(:my_second_instance_method).returns(:new_return_value)
|
75
|
+
object.unstub(:my_first_instance_method, :my_second_instance_method)
|
76
|
+
assert_equal :original_return_value, object.my_first_instance_method
|
77
|
+
assert_equal :original_return_value, object.my_second_instance_method
|
78
|
+
end
|
79
|
+
assert_passed(test_result)
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_unstubbing_a_method_multiple_times_should_restore_original_behaviour
|
83
|
+
klass = Class.new do
|
84
|
+
def my_instance_method; :original_return_value; end
|
85
|
+
end
|
86
|
+
test_result = run_as_test do
|
87
|
+
object = klass.new
|
88
|
+
object.stubs(:my_instance_method).returns(:new_return_value)
|
89
|
+
object.unstub(:my_instance_method)
|
90
|
+
object.unstub(:my_instance_method)
|
91
|
+
assert_equal :original_return_value, object.my_instance_method
|
92
|
+
end
|
93
|
+
assert_passed(test_result)
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_unstubbing_a_non_stubbed_method_should_do_nothing
|
97
|
+
klass = Class.new do
|
98
|
+
def my_instance_method; :original_return_value; end
|
99
|
+
end
|
100
|
+
test_result = run_as_test do
|
101
|
+
object = klass.new
|
102
|
+
object.unstub(:my_instance_method)
|
103
|
+
assert_equal :original_return_value, object.my_instance_method
|
104
|
+
end
|
105
|
+
assert_passed(test_result)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_unstubbing_a_method_which_was_stubbed_multiple_times_should_restore_orginal_behaviour
|
109
|
+
klass = Class.new do
|
110
|
+
def my_instance_method; :original_return_value; end
|
111
|
+
end
|
112
|
+
test_result = run_as_test do
|
113
|
+
object = klass.new
|
114
|
+
object.stubs(:my_instance_method).with(:first).returns(:first_new_return_value)
|
115
|
+
object.stubs(:my_instance_method).with(:second).returns(:second_new_return_value)
|
116
|
+
object.unstub(:my_instance_method)
|
117
|
+
assert_equal :original_return_value, object.my_instance_method
|
118
|
+
end
|
119
|
+
assert_passed(test_result)
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
data/test/unit/central_test.rb
CHANGED
@@ -47,6 +47,32 @@ class CentralTest < Test::Unit::TestCase
|
|
47
47
|
assert_equal [method], stubba.stubba_methods
|
48
48
|
end
|
49
49
|
|
50
|
+
def test_should_unstub_specified_method
|
51
|
+
stubba = Central.new
|
52
|
+
method_1 = Mock.new
|
53
|
+
method_2 = Mock.new
|
54
|
+
method_2.expects(:unstub)
|
55
|
+
stubba.stubba_methods = [method_1, method_2]
|
56
|
+
|
57
|
+
stubba.unstub(method_2)
|
58
|
+
|
59
|
+
assert_equal [method_1], stubba.stubba_methods
|
60
|
+
assert method_2.__verified__?
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_should_not_unstub_specified_method_if_not_already_stubbed
|
64
|
+
stubba = Central.new
|
65
|
+
method_1 = Mock.new
|
66
|
+
method_2 = Mock.new
|
67
|
+
method_2.expects(:unstub).never
|
68
|
+
stubba.stubba_methods = [method_1]
|
69
|
+
|
70
|
+
stubba.unstub(method_2)
|
71
|
+
|
72
|
+
assert_equal [method_1], stubba.stubba_methods
|
73
|
+
assert method_2.__verified__?
|
74
|
+
end
|
75
|
+
|
50
76
|
def test_should_unstub_all_methods
|
51
77
|
stubba = Central.new
|
52
78
|
method_1 = Mock.new
|
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: 47
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 9
|
9
|
-
-
|
10
|
-
version: 0.9.
|
9
|
+
- 10
|
10
|
+
version: 0.9.10
|
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: 2010-
|
18
|
+
date: 2010-11-25 00:00:00 +00:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- test/acceptance/bug_18914_test.rb
|
127
127
|
- test/acceptance/bug_21465_test.rb
|
128
128
|
- test/acceptance/bug_21563_test.rb
|
129
|
+
- test/acceptance/exception_rescue_test.rb
|
129
130
|
- test/acceptance/expected_invocation_count_test.rb
|
130
131
|
- test/acceptance/failure_messages_test.rb
|
131
132
|
- test/acceptance/minitest_test.rb
|
@@ -158,6 +159,7 @@ files:
|
|
158
159
|
- test/acceptance/stubbing_non_public_class_method_test.rb
|
159
160
|
- test/acceptance/stubbing_non_public_instance_method_test.rb
|
160
161
|
- test/acceptance/stubbing_on_non_mock_object_test.rb
|
162
|
+
- test/acceptance/unstubbing_test.rb
|
161
163
|
- test/deprecation_disabler.rb
|
162
164
|
- test/execution_point.rb
|
163
165
|
- test/method_definer.rb
|