mocha 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +17 -146
- data/Rakefile +60 -6
- data/lib/mocha.rb +1 -1
- data/lib/mocha/auto_verify.rb +101 -34
- data/lib/mocha/expectation.rb +112 -3
- data/lib/mocha/mock.rb +9 -2
- data/lib/mocha/mock_methods.rb +21 -1
- data/lib/smart_test_case.rb +1 -1
- data/lib/smart_test_case/multiple_setup_and_teardown.rb +91 -89
- data/lib/stubba.rb +1 -1
- data/lib/stubba/object.rb +33 -8
- data/lib/stubba/setup_and_teardown.rb +19 -17
- data/test/all_tests.rb +0 -7
- data/test/mocha/auto_verify_test.rb +16 -1
- data/test/mocha/expectation_test.rb +8 -6
- data/test/mocha/mock_methods_test.rb +1 -1
- data/test/mocha/mock_test.rb +10 -0
- data/test/mocha_test_result_integration_test.rb +2 -2
- data/test/smart_test_case/multiple_setup_and_teardown_test.rb +11 -11
- data/test/stubba/setup_and_teardown_test.rb +3 -3
- data/test/stubba_integration_test.rb +7 -7
- data/test/stubba_test_result_integration_test.rb +2 -2
- metadata +3 -12
- data/TODO +0 -14
- data/lib/auto_mocha.rb +0 -2
- data/lib/auto_mocha/auto_mock.rb +0 -54
- data/lib/auto_mocha/mock_class.rb +0 -38
- data/test/auto_mocha/auto_mock_test.rb +0 -85
- data/test/auto_mocha/mock_class_test.rb +0 -179
- data/test/auto_mock_acceptance_test.rb +0 -35
data/lib/mocha/mock.rb
CHANGED
@@ -4,9 +4,16 @@ module Mocha
|
|
4
4
|
class Mock
|
5
5
|
|
6
6
|
include MockMethods
|
7
|
-
|
8
|
-
|
7
|
+
|
8
|
+
attr_reader :__mock_name
|
9
|
+
def initialize(stub_everything = false, name = nil)
|
9
10
|
@stub_everything = stub_everything
|
11
|
+
@__mock_name = name
|
12
|
+
end
|
13
|
+
|
14
|
+
alias :mocha_inspect_before_hijacked_by_named_mocks :mocha_inspect
|
15
|
+
def mocha_inspect
|
16
|
+
@__mock_name ? "#<Mock: '#{@__mock_name}'>" : mocha_inspect_before_hijacked_by_named_mocks
|
10
17
|
end
|
11
18
|
|
12
19
|
end
|
data/lib/mocha/mock_methods.rb
CHANGED
@@ -1,24 +1,38 @@
|
|
1
1
|
require 'mocha/expectation'
|
2
2
|
|
3
3
|
module Mocha
|
4
|
+
# Methods added to mock objects.
|
5
|
+
# These methods all return an expectation which can be further modified by methods on Mocha::Expectation.
|
4
6
|
module MockMethods
|
5
7
|
|
8
|
+
# :stopdoc:
|
9
|
+
|
6
10
|
attr_reader :stub_everything
|
7
11
|
|
8
12
|
def expectations
|
9
13
|
@expectations ||= []
|
10
14
|
end
|
11
15
|
|
16
|
+
# :startdoc:
|
17
|
+
|
18
|
+
# :call-seq: expects(symbol) -> expectation
|
19
|
+
#
|
20
|
+
# Adds an expectation that a method identified by +symbol+ must be called exactly once with any parameters.
|
12
21
|
def expects(symbol, backtrace = nil)
|
13
22
|
expectations << Expectation.new(symbol, backtrace)
|
14
23
|
expectations.last
|
15
24
|
end
|
16
25
|
|
26
|
+
# :call-seq: stubs(symbol) -> expectation
|
27
|
+
#
|
28
|
+
# Adds an expectation that a method identified by +symbol+ may be called any number of times with any parameters.
|
17
29
|
def stubs(symbol, backtrace = nil)
|
18
30
|
expectations << Stub.new(symbol, backtrace)
|
19
31
|
expectations.last
|
20
32
|
end
|
21
33
|
|
34
|
+
# :stopdoc:
|
35
|
+
|
22
36
|
def method_missing(symbol, *arguments, &block)
|
23
37
|
matching_expectation = matching_expectation(symbol, *arguments)
|
24
38
|
if matching_expectation then
|
@@ -43,13 +57,19 @@ module Mocha
|
|
43
57
|
end
|
44
58
|
|
45
59
|
def unexpected_method_called(symbol, *arguments)
|
46
|
-
MissingExpectation.new(symbol, expectations).with(*arguments).verify
|
60
|
+
MissingExpectation.new(symbol, self, expectations).with(*arguments).verify
|
47
61
|
end
|
48
62
|
|
49
63
|
def matching_expectation(symbol, *arguments)
|
50
64
|
expectations.detect { |expectation| expectation.match?(symbol, *arguments) }
|
51
65
|
end
|
52
66
|
|
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.
|
53
73
|
def verify(&block)
|
54
74
|
expectations.each { |expectation| expectation.verify(&block) }
|
55
75
|
end
|
data/lib/smart_test_case.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'smart_test_case/multiple_setup_and_teardown'
|
2
2
|
|
3
3
|
class Test::Unit::TestCase
|
4
|
-
include MultipleSetupAndTeardown unless ancestors.include?(MultipleSetupAndTeardown)
|
4
|
+
include SmartTestCase::MultipleSetupAndTeardown unless ancestors.include?(SmartTestCase::MultipleSetupAndTeardown)
|
5
5
|
end
|
@@ -1,123 +1,125 @@
|
|
1
|
-
module
|
1
|
+
module SmartTestCase
|
2
|
+
module MultipleSetupAndTeardown
|
2
3
|
|
3
|
-
|
4
|
+
def self.included(base)
|
4
5
|
|
5
|
-
|
6
|
+
base.class_eval do
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def self.method_added(symbol)
|
9
|
+
# disable until later
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
if method_defined?(:setup) then
|
13
|
+
alias_method :setup_original, :setup
|
14
|
+
define_method(:setup_new) do
|
15
|
+
begin
|
16
|
+
setup_original
|
17
|
+
ensure
|
18
|
+
setup_mocha
|
19
|
+
end
|
20
|
+
end
|
21
|
+
else
|
22
|
+
define_method(:setup_new) do
|
17
23
|
setup_mocha
|
18
24
|
end
|
19
25
|
end
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
alias_method :setup, :setup_new
|
27
|
+
|
28
|
+
if method_defined?(:teardown) then
|
29
|
+
alias_method :teardown_original, :teardown
|
30
|
+
define_method(:teardown_new) do
|
31
|
+
begin
|
32
|
+
teardown_mocha
|
33
|
+
ensure
|
34
|
+
teardown_original
|
35
|
+
end
|
36
|
+
end
|
37
|
+
else
|
38
|
+
define_method(:teardown_new) do
|
31
39
|
teardown_mocha
|
32
|
-
ensure
|
33
|
-
teardown_original
|
34
40
|
end
|
35
41
|
end
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
begin
|
50
|
-
setup_new
|
51
|
-
ensure
|
52
|
-
setup_added
|
42
|
+
alias_method :teardown, :teardown_new
|
43
|
+
|
44
|
+
def self.method_added(method)
|
45
|
+
case method
|
46
|
+
when :setup
|
47
|
+
unless method_defined?(:setup_added)
|
48
|
+
alias_method :setup_added, :setup
|
49
|
+
define_method(:setup) do
|
50
|
+
begin
|
51
|
+
setup_new
|
52
|
+
ensure
|
53
|
+
setup_added
|
54
|
+
end
|
53
55
|
end
|
54
56
|
end
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
57
|
+
when :teardown
|
58
|
+
unless method_defined?(:teardown_added)
|
59
|
+
alias_method :teardown_added, :teardown
|
60
|
+
define_method(:teardown) do
|
61
|
+
begin
|
62
|
+
teardown_added
|
63
|
+
ensure
|
64
|
+
teardown_new
|
65
|
+
end
|
64
66
|
end
|
65
67
|
end
|
66
68
|
end
|
67
69
|
end
|
68
|
-
end
|
69
70
|
|
70
|
-
|
71
|
+
class << self
|
71
72
|
|
72
|
-
|
73
|
-
|
74
|
-
|
73
|
+
def setup_methods
|
74
|
+
@setup_methods ||= []
|
75
|
+
end
|
75
76
|
|
76
|
-
|
77
|
-
|
78
|
-
|
77
|
+
def teardown_methods
|
78
|
+
@teardown_methods ||= []
|
79
|
+
end
|
79
80
|
|
80
|
-
|
81
|
-
|
82
|
-
|
81
|
+
def add_setup_method(symbol)
|
82
|
+
setup_methods << symbol
|
83
|
+
end
|
83
84
|
|
84
|
-
|
85
|
-
|
86
|
-
|
85
|
+
def add_teardown_method(symbol)
|
86
|
+
teardown_methods << symbol
|
87
|
+
end
|
87
88
|
|
88
|
-
|
89
|
+
private
|
89
90
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
91
|
+
def inherited_with_setup_and_teardown_methods(child)
|
92
|
+
inherited_without_setup_and_teardown_methods(child) if respond_to?(:inherited_without_setup_and_teardown_methods, true)
|
93
|
+
child.instance_variable_set('@setup_methods', setup_methods.dup)
|
94
|
+
child.instance_variable_set('@teardown_methods', teardown_methods.dup)
|
95
|
+
end
|
95
96
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
97
|
+
if respond_to?(:inherited, true)
|
98
|
+
alias_method :inherited_without_setup_and_teardown_methods, :inherited
|
99
|
+
end
|
100
|
+
alias_method :inherited, :inherited_with_setup_and_teardown_methods
|
100
101
|
|
101
|
-
|
102
|
+
end
|
102
103
|
|
103
|
-
|
104
|
-
|
105
|
-
|
104
|
+
def setup_mocha
|
105
|
+
self.class.class_eval { setup_methods }.each { |symbol| send(symbol) }
|
106
|
+
end
|
106
107
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
108
|
+
def teardown_mocha
|
109
|
+
stored_exception = nil
|
110
|
+
self.class.class_eval { teardown_methods }.reverse.each do |symbol|
|
111
|
+
begin
|
112
|
+
send(symbol)
|
113
|
+
rescue Exception => e
|
114
|
+
stored_exception ||= e
|
115
|
+
end
|
114
116
|
end
|
117
|
+
raise stored_exception if stored_exception
|
115
118
|
end
|
116
|
-
|
119
|
+
|
117
120
|
end
|
118
121
|
|
119
122
|
end
|
120
123
|
|
121
124
|
end
|
122
|
-
|
123
|
-
end
|
125
|
+
end
|
data/lib/stubba.rb
CHANGED
data/lib/stubba/object.rb
CHANGED
@@ -3,43 +3,60 @@ require 'stubba/instance_method'
|
|
3
3
|
require 'stubba/class_method'
|
4
4
|
require 'stubba/any_instance_method'
|
5
5
|
|
6
|
+
# Methods added all Objects.
|
6
7
|
class Object
|
7
8
|
|
8
|
-
def mocha
|
9
|
+
def mocha # :nodoc:
|
9
10
|
@mocha ||= Mocha::Mock.new
|
10
11
|
end
|
11
12
|
|
12
|
-
def reset_mocha
|
13
|
+
def reset_mocha # :nodoc:
|
13
14
|
@mocha = nil
|
14
15
|
end
|
15
16
|
|
16
|
-
def stubba_method
|
17
|
+
def stubba_method # :nodoc:
|
17
18
|
Stubba::InstanceMethod
|
18
19
|
end
|
19
20
|
|
20
|
-
def stubba_object
|
21
|
+
def stubba_object # :nodoc:
|
21
22
|
self
|
22
23
|
end
|
23
|
-
|
24
|
+
|
25
|
+
# :call-seq: expects(symbol) -> expectation
|
26
|
+
#
|
27
|
+
# Adds an expectation that a method identified by +symbol+ must be called exactly once with any parameters.
|
28
|
+
# Returns the new expectation which can be further modified by methods on Mocha::Expectation.
|
29
|
+
# product = Product.new
|
30
|
+
# product.expects(:save).returns(true)
|
31
|
+
# assert_equal false, product.save
|
24
32
|
def expects(symbol)
|
25
33
|
method = stubba_method.new(stubba_object, symbol)
|
26
34
|
$stubba.stub(method)
|
27
35
|
mocha.expects(symbol, caller)
|
28
36
|
end
|
29
37
|
|
38
|
+
# :call-seq: stubs(symbol) -> expectation
|
39
|
+
#
|
40
|
+
# Adds an expectation that a method identified by +symbol+ may be called any number of times with any parameters.
|
41
|
+
# Returns the new expectation which can be further modified by methods on Mocha::Expectation.
|
42
|
+
# product = Product.new
|
43
|
+
# product.stubs(:save).returns(true)
|
44
|
+
# assert_equal false, product.save
|
30
45
|
def stubs(symbol)
|
31
46
|
method = stubba_method.new(stubba_object, symbol)
|
32
47
|
$stubba.stub(method)
|
33
48
|
mocha.stubs(symbol, caller)
|
34
49
|
end
|
35
50
|
|
51
|
+
# Asserts that all expectations have been fulfilled.
|
52
|
+
# Called automatically at the end of a test for all objects with expectations.
|
36
53
|
def verify
|
37
54
|
mocha.verify
|
38
55
|
end
|
39
56
|
|
40
57
|
end
|
41
58
|
|
42
|
-
class Module
|
59
|
+
class Module # :nodoc:
|
43
60
|
|
44
61
|
def stubba_method
|
45
62
|
Stubba::ClassMethod
|
@@ -49,11 +66,11 @@ end
|
|
49
66
|
|
50
67
|
class Class
|
51
68
|
|
52
|
-
def stubba_method
|
69
|
+
def stubba_method # :nodoc:
|
53
70
|
Stubba::ClassMethod
|
54
71
|
end
|
55
72
|
|
56
|
-
class AnyInstance
|
73
|
+
class AnyInstance # :nodoc:
|
57
74
|
|
58
75
|
def initialize(klass)
|
59
76
|
@stubba_object = klass
|
@@ -69,6 +86,14 @@ class Class
|
|
69
86
|
|
70
87
|
end
|
71
88
|
|
89
|
+
# :call-seq: any_instance -> mock object
|
90
|
+
#
|
91
|
+
# Returns a mock object which will detect calls to any instance of this class.
|
92
|
+
# Product.any_instance.stubs(:save).returns(false)
|
93
|
+
# product_1 = Product.new
|
94
|
+
# assert_equal false, product_1.save
|
95
|
+
# product_2 = Product.new
|
96
|
+
# assert_equal false, product_2.save
|
72
97
|
def any_instance
|
73
98
|
@any_instance ||= AnyInstance.new(self)
|
74
99
|
end
|
@@ -1,25 +1,27 @@
|
|
1
1
|
require 'stubba/central'
|
2
2
|
|
3
|
-
module
|
3
|
+
module Stubba
|
4
|
+
module SetupAndTeardown
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
def self.included(base)
|
7
|
+
base.add_setup_method(:setup_stubs)
|
8
|
+
base.add_teardown_method(:teardown_stubs)
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
def setup_stubs
|
12
|
+
$stubba = Stubba::Central.new
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
def teardown_stubs
|
16
|
+
if $stubba then
|
17
|
+
begin
|
18
|
+
$stubba.verify_all { add_assertion }
|
19
|
+
ensure
|
20
|
+
$stubba.unstub_all
|
21
|
+
$stubba = nil
|
22
|
+
end
|
21
23
|
end
|
22
24
|
end
|
23
|
-
end
|
24
25
|
|
25
|
-
end
|
26
|
+
end
|
27
|
+
end
|