mocha 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +4 -4
- data/RELEASE +32 -0
- data/Rakefile +3 -3
- data/examples/misc.rb +36 -0
- data/examples/mocha.rb +26 -0
- data/examples/stubba.rb +65 -0
- data/lib/mocha.rb +17 -5
- data/lib/{stubba → mocha}/any_instance_method.rb +2 -2
- data/lib/mocha/auto_verify.rb +7 -7
- data/lib/{stubba → mocha}/central.rb +2 -2
- data/lib/{stubba → mocha}/class_method.rb +7 -10
- data/lib/mocha/expectation.rb +88 -33
- data/lib/mocha/expectation_error.rb +6 -0
- data/lib/mocha/inspect.rb +1 -1
- data/lib/mocha/instance_method.rb +8 -0
- data/lib/mocha/metaclass.rb +1 -1
- data/lib/mocha/mock.rb +8 -8
- data/lib/mocha/mock_methods.rb +60 -16
- data/lib/{stubba → mocha}/object.rb +8 -10
- data/lib/mocha/setup_and_teardown.rb +23 -0
- data/lib/mocha/standalone.rb +30 -0
- data/lib/mocha/test_case_adapter.rb +49 -0
- data/lib/mocha_standalone.rb +2 -0
- data/lib/stubba.rb +2 -8
- data/test/all_tests.rb +9 -10
- data/test/method_definer.rb +2 -2
- data/test/{stubba → mocha}/any_instance_method_test.rb +1 -3
- data/test/mocha/auto_verify_test.rb +9 -10
- data/test/{stubba → mocha}/central_test.rb +5 -4
- data/test/{stubba → mocha}/class_method_test.rb +40 -10
- data/test/mocha/expectation_test.rb +144 -67
- data/test/mocha/inspect_test.rb +12 -1
- data/test/mocha/metaclass_test.rb +22 -0
- data/test/mocha/mock_methods_test.rb +65 -7
- data/test/mocha/mock_test.rb +41 -4
- data/test/{stubba → mocha}/object_test.rb +9 -9
- data/test/mocha/setup_and_teardown_test.rb +76 -0
- data/test/mocha_acceptance_test.rb +8 -8
- data/test/mocha_test_result_integration_test.rb +5 -6
- data/test/standalone_acceptance_test.rb +110 -0
- data/test/stubba_acceptance_test.rb +2 -2
- data/test/stubba_integration_test.rb +17 -24
- data/test/stubba_test_result_integration_test.rb +5 -6
- metadata +22 -18
- data/lib/shared/backtracefilter.rb +0 -46
- data/lib/smart_test_case.rb +0 -5
- data/lib/smart_test_case/multiple_setup_and_teardown.rb +0 -123
- data/lib/stubba/instance_method.rb +0 -18
- data/lib/stubba/setup_and_teardown.rb +0 -25
- data/test/smart_test_case/multiple_setup_and_teardown_test.rb +0 -192
- data/test/stubba/instance_method_test.rb +0 -46
- data/test/stubba/setup_and_teardown_test.rb +0 -134
data/lib/mocha/inspect.rb
CHANGED
data/lib/mocha/metaclass.rb
CHANGED
data/lib/mocha/mock.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
require 'mocha/mock_methods'
|
2
2
|
|
3
3
|
module Mocha
|
4
|
+
|
4
5
|
class Mock
|
5
|
-
|
6
|
-
include MockMethods
|
7
6
|
|
8
|
-
|
7
|
+
include MockMethods
|
8
|
+
|
9
9
|
def initialize(stub_everything = false, name = nil)
|
10
10
|
@stub_everything = stub_everything
|
11
|
-
@
|
11
|
+
@mock_name = name
|
12
12
|
end
|
13
|
-
|
14
|
-
alias :mocha_inspect_before_hijacked_by_named_mocks :mocha_inspect
|
13
|
+
|
15
14
|
def mocha_inspect
|
16
|
-
@
|
15
|
+
@mock_name ? "#<Mock:#{@mock_name}>" : "#<Mock:0x#{__id__.to_s(16)}>"
|
17
16
|
end
|
18
|
-
|
17
|
+
|
19
18
|
end
|
19
|
+
|
20
20
|
end
|
data/lib/mocha/mock_methods.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'mocha/expectation'
|
2
|
+
require 'mocha/metaclass'
|
2
3
|
|
3
4
|
module Mocha
|
4
5
|
# Methods added to mock objects.
|
@@ -15,22 +16,69 @@ module Mocha
|
|
15
16
|
|
16
17
|
# :startdoc:
|
17
18
|
|
18
|
-
# :call-seq: expects(
|
19
|
+
# :call-seq: expects(method_name) -> expectation
|
20
|
+
# expects(method_names) -> last expectation
|
19
21
|
#
|
20
|
-
# Adds an expectation that a method identified by +
|
21
|
-
|
22
|
-
|
22
|
+
# Adds an expectation that a method identified by +method_name+ symbol must be called exactly once with any parameters.
|
23
|
+
# Returns the new expectation which can be further modified by methods on Mocha::Expectation.
|
24
|
+
# object = mock()
|
25
|
+
# object.expects(:method1)
|
26
|
+
# object.method1
|
27
|
+
# # no error raised
|
28
|
+
#
|
29
|
+
# object = mock()
|
30
|
+
# object.expects(:method1)
|
31
|
+
# # error raised, because method1 not called exactly once
|
32
|
+
# If +method_names+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
|
33
|
+
# object = mock()
|
34
|
+
# object.expects(:method1 => :result1, :method2 => :result2)
|
35
|
+
#
|
36
|
+
# # exactly equivalent to
|
37
|
+
#
|
38
|
+
# object = mock()
|
39
|
+
# object.expects(:method1).returns(:result1)
|
40
|
+
# object.expects(:method2).returns(:result2)
|
41
|
+
def expects(method_names, backtrace = nil)
|
42
|
+
method_names = method_names.is_a?(Hash) ? method_names : { method_names => nil }
|
43
|
+
method_names.each do |method_name, return_value|
|
44
|
+
expectations << Expectation.new(self, method_name, backtrace).returns(return_value)
|
45
|
+
self.__metaclass__.send(:undef_method, method_name) if self.__metaclass__.method_defined?(method_name)
|
46
|
+
end
|
23
47
|
expectations.last
|
24
48
|
end
|
25
49
|
|
26
|
-
|
50
|
+
alias_method :__expects__, :expects
|
51
|
+
|
52
|
+
# :call-seq: stubs(method_name) -> expectation
|
53
|
+
# stubs(method_names) -> last expectation
|
54
|
+
#
|
55
|
+
# Adds an expectation that a method identified by +method_name+ symbol may be called any number of times with any parameters.
|
56
|
+
# Returns the new expectation which can be further modified by methods on Mocha::Expectation.
|
57
|
+
# object = mock()
|
58
|
+
# object.stubs(:method1)
|
59
|
+
# object.method1
|
60
|
+
# object.method1
|
61
|
+
# # no error raised
|
62
|
+
# If +method_names+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
|
63
|
+
# object = mock()
|
64
|
+
# object.stubs(:method1 => :result1, :method2 => :result2)
|
65
|
+
#
|
66
|
+
# # exactly equivalent to
|
27
67
|
#
|
28
|
-
#
|
29
|
-
|
30
|
-
|
68
|
+
# object = mock()
|
69
|
+
# object.stubs(:method1).returns(:result1)
|
70
|
+
# object.stubs(:method2).returns(:result2)
|
71
|
+
def stubs(method_names, backtrace = nil)
|
72
|
+
method_names = method_names.is_a?(Hash) ? method_names : { method_names => nil }
|
73
|
+
method_names.each do |method_name, return_value|
|
74
|
+
expectations << Stub.new(self, method_name, backtrace).returns(return_value)
|
75
|
+
self.__metaclass__.send(:undef_method, method_name) if self.__metaclass__.method_defined?(method_name)
|
76
|
+
end
|
31
77
|
expectations.last
|
32
78
|
end
|
33
79
|
|
80
|
+
alias_method :__stubs__, :stubs
|
81
|
+
|
34
82
|
# :stopdoc:
|
35
83
|
|
36
84
|
def method_missing(symbol, *arguments, &block)
|
@@ -57,22 +105,18 @@ module Mocha
|
|
57
105
|
end
|
58
106
|
|
59
107
|
def unexpected_method_called(symbol, *arguments)
|
60
|
-
MissingExpectation.new(
|
108
|
+
MissingExpectation.new(self, symbol).with(*arguments).verify
|
61
109
|
end
|
62
110
|
|
63
111
|
def matching_expectation(symbol, *arguments)
|
64
|
-
expectations.detect { |expectation| expectation.match?(symbol, *arguments) }
|
112
|
+
expectations.reverse.detect { |expectation| expectation.match?(symbol, *arguments) }
|
65
113
|
end
|
66
114
|
|
67
|
-
# :startdoc:
|
68
|
-
|
69
|
-
# :call-seq: verify
|
70
|
-
#
|
71
|
-
# Asserts that all expectations have been fulfilled.
|
72
|
-
# Called automatically at the end of a test for mock objects created by methods in Mocha::AutoVerify.
|
73
115
|
def verify(&block)
|
74
116
|
expectations.each { |expectation| expectation.verify(&block) }
|
75
117
|
end
|
76
118
|
|
119
|
+
# :startdoc:
|
120
|
+
|
77
121
|
end
|
78
122
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'mocha/mock'
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
require '
|
2
|
+
require 'mocha/instance_method'
|
3
|
+
require 'mocha/class_method'
|
4
|
+
require 'mocha/any_instance_method'
|
5
5
|
|
6
6
|
# Methods added all Objects.
|
7
7
|
class Object
|
@@ -15,7 +15,7 @@ class Object
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def stubba_method # :nodoc:
|
18
|
-
|
18
|
+
Mocha::InstanceMethod
|
19
19
|
end
|
20
20
|
|
21
21
|
def stubba_object # :nodoc:
|
@@ -48,9 +48,7 @@ class Object
|
|
48
48
|
mocha.stubs(symbol, caller)
|
49
49
|
end
|
50
50
|
|
51
|
-
|
52
|
-
# Called automatically at the end of a test for all objects with expectations.
|
53
|
-
def verify
|
51
|
+
def verify # :nodoc:
|
54
52
|
mocha.verify
|
55
53
|
end
|
56
54
|
|
@@ -59,7 +57,7 @@ end
|
|
59
57
|
class Module # :nodoc:
|
60
58
|
|
61
59
|
def stubba_method
|
62
|
-
|
60
|
+
Mocha::ClassMethod
|
63
61
|
end
|
64
62
|
|
65
63
|
end
|
@@ -67,7 +65,7 @@ end
|
|
67
65
|
class Class
|
68
66
|
|
69
67
|
def stubba_method # :nodoc:
|
70
|
-
|
68
|
+
Mocha::ClassMethod
|
71
69
|
end
|
72
70
|
|
73
71
|
class AnyInstance # :nodoc:
|
@@ -77,7 +75,7 @@ class Class
|
|
77
75
|
end
|
78
76
|
|
79
77
|
def stubba_method
|
80
|
-
|
78
|
+
Mocha::AnyInstanceMethod
|
81
79
|
end
|
82
80
|
|
83
81
|
def stubba_object
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'mocha/central'
|
2
|
+
|
3
|
+
module Mocha
|
4
|
+
|
5
|
+
module SetupAndTeardown
|
6
|
+
|
7
|
+
def setup_stubs
|
8
|
+
$stubba = Mocha::Central.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def verify_stubs
|
12
|
+
$stubba.verify_all { yield if block_given? } if $stubba
|
13
|
+
end
|
14
|
+
|
15
|
+
def teardown_stubs
|
16
|
+
if $stubba then
|
17
|
+
$stubba.unstub_all
|
18
|
+
$stubba = nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'mocha/auto_verify'
|
2
|
+
require 'mocha/setup_and_teardown'
|
3
|
+
|
4
|
+
module Mocha
|
5
|
+
|
6
|
+
module Standalone
|
7
|
+
|
8
|
+
include AutoVerify
|
9
|
+
include SetupAndTeardown
|
10
|
+
|
11
|
+
def mocha_setup
|
12
|
+
setup_stubs
|
13
|
+
end
|
14
|
+
|
15
|
+
def mocha_verify(&block)
|
16
|
+
verify_mocks(&block)
|
17
|
+
verify_stubs(&block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def mocha_teardown
|
21
|
+
begin
|
22
|
+
teardown_mocks
|
23
|
+
ensure
|
24
|
+
teardown_stubs
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'mocha/expectation_error'
|
2
|
+
|
3
|
+
module Mocha
|
4
|
+
|
5
|
+
module TestCaseAdapter
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.class_eval do
|
9
|
+
|
10
|
+
alias_method :run_before_mocha_test_case_adapter, :run
|
11
|
+
|
12
|
+
def run(result)
|
13
|
+
yield(Test::Unit::TestCase::STARTED, name)
|
14
|
+
@_result = result
|
15
|
+
begin
|
16
|
+
mocha_setup
|
17
|
+
begin
|
18
|
+
setup
|
19
|
+
__send__(@method_name)
|
20
|
+
mocha_verify { add_assertion }
|
21
|
+
rescue Mocha::ExpectationError => e
|
22
|
+
add_failure(e.message, e.backtrace)
|
23
|
+
rescue Test::Unit::AssertionFailedError => e
|
24
|
+
add_failure(e.message, e.backtrace)
|
25
|
+
rescue StandardError, ScriptError
|
26
|
+
add_error($!)
|
27
|
+
ensure
|
28
|
+
begin
|
29
|
+
teardown
|
30
|
+
rescue Test::Unit::AssertionFailedError => e
|
31
|
+
add_failure(e.message, e.backtrace)
|
32
|
+
rescue StandardError, ScriptError
|
33
|
+
add_error($!)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
ensure
|
37
|
+
mocha_teardown
|
38
|
+
end
|
39
|
+
result.add_run
|
40
|
+
yield(Test::Unit::TestCase::FINISHED, name)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
data/lib/stubba.rb
CHANGED
@@ -1,8 +1,2 @@
|
|
1
|
-
|
2
|
-
require '
|
3
|
-
require 'stubba/setup_and_teardown'
|
4
|
-
require 'shared/backtracefilter'
|
5
|
-
|
6
|
-
class Test::Unit::TestCase
|
7
|
-
include SetupAndTeardown unless ancestors.include?(SetupAndTeardown)
|
8
|
-
end
|
1
|
+
# for backwards compatibility
|
2
|
+
require 'mocha'
|
data/test/all_tests.rb
CHANGED
@@ -8,14 +8,12 @@ require 'mocha/mock_methods_test'
|
|
8
8
|
require 'mocha/mock_test'
|
9
9
|
require 'mocha/auto_verify_test'
|
10
10
|
|
11
|
-
require '
|
12
|
-
require '
|
13
|
-
require '
|
14
|
-
require '
|
15
|
-
require '
|
16
|
-
require '
|
17
|
-
|
18
|
-
require 'smart_test_case/multiple_setup_and_teardown_test'
|
11
|
+
require 'mocha/central_test'
|
12
|
+
require 'mocha/class_method_test'
|
13
|
+
require 'mocha/any_instance_method_test'
|
14
|
+
require 'mocha/setup_and_teardown_test'
|
15
|
+
require 'mocha/object_test'
|
16
|
+
require 'mocha/metaclass_test'
|
19
17
|
|
20
18
|
class UnitTests
|
21
19
|
|
@@ -30,11 +28,10 @@ class UnitTests
|
|
30
28
|
suite << AutoVerifyTest.suite
|
31
29
|
suite << CentralTest.suite
|
32
30
|
suite << ClassMethodTest.suite
|
33
|
-
suite << InstanceMethodTest.suite
|
34
31
|
suite << AnyInstanceMethodTest.suite
|
35
32
|
suite << SetupAndTeardownTest.suite
|
36
33
|
suite << ObjectTest.suite
|
37
|
-
suite <<
|
34
|
+
suite << MetaclassTest.suite
|
38
35
|
suite
|
39
36
|
end
|
40
37
|
|
@@ -61,6 +58,7 @@ Test::Unit::UI::Console::TestRunner.run(IntegrationTests)
|
|
61
58
|
|
62
59
|
require 'mocha_acceptance_test'
|
63
60
|
require 'stubba_acceptance_test'
|
61
|
+
require 'standalone_acceptance_test'
|
64
62
|
|
65
63
|
class AcceptanceTests
|
66
64
|
|
@@ -68,6 +66,7 @@ class AcceptanceTests
|
|
68
66
|
suite = Test::Unit::TestSuite.new('AcceptanceTests')
|
69
67
|
suite << MochaAcceptanceTest.suite
|
70
68
|
suite << StubbaAcceptanceTest.suite
|
69
|
+
suite << StandaloneAcceptanceTest.suite
|
71
70
|
suite
|
72
71
|
end
|
73
72
|
|
data/test/method_definer.rb
CHANGED
@@ -3,7 +3,7 @@ require 'mocha/metaclass'
|
|
3
3
|
class Object
|
4
4
|
|
5
5
|
def define_instance_method(method_symbol, &block)
|
6
|
-
|
6
|
+
__metaclass__.send(:define_method, method_symbol, block)
|
7
7
|
end
|
8
8
|
|
9
9
|
def replace_instance_method(method_symbol, &block)
|
@@ -12,7 +12,7 @@ class Object
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def define_instance_accessor(*symbols)
|
15
|
-
symbols.each { |symbol|
|
15
|
+
symbols.each { |symbol| __metaclass__.send(:attr_accessor, symbol) }
|
16
16
|
end
|
17
17
|
|
18
18
|
end
|
@@ -1,12 +1,10 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
2
|
require 'method_definer'
|
3
3
|
require 'mocha/mock'
|
4
|
-
|
5
|
-
require 'stubba/any_instance_method'
|
4
|
+
require 'mocha/any_instance_method'
|
6
5
|
|
7
6
|
class AnyInstanceMethodTest < Test::Unit::TestCase
|
8
7
|
|
9
|
-
include Stubba
|
10
8
|
include Mocha
|
11
9
|
|
12
10
|
def test_should_hide_original_method
|
@@ -95,7 +95,7 @@ class AutoVerifyTest < Test::Unit::TestCase
|
|
95
95
|
assert_equal expected, test_case.mocks
|
96
96
|
end
|
97
97
|
|
98
|
-
def
|
98
|
+
def test_should_verify_each_mock
|
99
99
|
mocks = Array.new(3) do
|
100
100
|
mock = Object.new
|
101
101
|
mock.define_instance_accessor(:verify_called)
|
@@ -103,20 +103,19 @@ class AutoVerifyTest < Test::Unit::TestCase
|
|
103
103
|
mock
|
104
104
|
end
|
105
105
|
test_case.replace_instance_method(:mocks) { mocks }
|
106
|
-
test_case.
|
106
|
+
test_case.verify_mocks
|
107
107
|
assert mocks.all? { |mock| mock.verify_called }
|
108
108
|
end
|
109
109
|
|
110
|
-
def
|
110
|
+
def test_should_yield_to_block_for_each_assertion
|
111
111
|
mock_class = Class.new do
|
112
112
|
def verify(&block); yield; end
|
113
113
|
end
|
114
114
|
mock = mock_class.new
|
115
115
|
test_case.replace_instance_method(:mocks) { [mock] }
|
116
|
-
|
117
|
-
test_case.
|
118
|
-
|
119
|
-
assert test_case.add_assertion_called
|
116
|
+
yielded = false
|
117
|
+
test_case.verify_mocks { yielded = true }
|
118
|
+
assert yielded
|
120
119
|
end
|
121
120
|
|
122
121
|
def test_should_reset_mocks_on_teardown
|
@@ -148,17 +147,17 @@ class AutoVerifyTest < Test::Unit::TestCase
|
|
148
147
|
|
149
148
|
def test_should_create_mock_with_name
|
150
149
|
mock = test_case.mock('named_mock')
|
151
|
-
assert_equal 'named_mock', mock.
|
150
|
+
assert_equal '#<Mock:named_mock>', mock.mocha_inspect
|
152
151
|
end
|
153
152
|
|
154
153
|
def test_should_create_stub_with_name
|
155
154
|
stub = test_case.stub('named_stub')
|
156
|
-
assert_equal 'named_stub', stub.
|
155
|
+
assert_equal '#<Mock:named_stub>', stub.mocha_inspect
|
157
156
|
end
|
158
157
|
|
159
158
|
def test_should_create_greedy_stub_with_name
|
160
159
|
greedy_stub = test_case.stub_everything('named_greedy_stub')
|
161
|
-
assert_equal 'named_greedy_stub', greedy_stub.
|
160
|
+
assert_equal '#<Mock:named_greedy_stub>', greedy_stub.mocha_inspect
|
162
161
|
end
|
163
162
|
|
164
163
|
end
|