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/test/mocha/inspect_test.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
2
|
require 'mocha/inspect'
|
3
|
+
require 'method_definer'
|
3
4
|
|
4
5
|
class InspectTest
|
5
6
|
def self.suite
|
@@ -17,7 +18,17 @@ class ObjectInspectTest < Test::Unit::TestCase
|
|
17
18
|
|
18
19
|
def test_should_provide_custom_representation_of_object
|
19
20
|
object = Object.new
|
20
|
-
assert_equal "#<#{object.class}
|
21
|
+
assert_equal "#<#{object.class}:#{"0x%x" % object.__id__}>", object.mocha_inspect
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_should_use_underscored_id_instead_of_object_id_or_id_so_that_they_can_be_stubbed
|
25
|
+
object = Object.new
|
26
|
+
object.define_instance_accessor(:called)
|
27
|
+
object.called = false
|
28
|
+
object.replace_instance_method(:object_id) { self.called = true; 1 }
|
29
|
+
object.replace_instance_method(:id) { self.called = true; 1 }
|
30
|
+
object.mocha_inspect
|
31
|
+
assert_equal false, object.called
|
21
32
|
end
|
22
33
|
|
23
34
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
|
+
require 'mocha/metaclass'
|
3
|
+
|
4
|
+
class MetaclassTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_should_return_objects_singleton_class
|
7
|
+
object = Object.new
|
8
|
+
assert_raises(NoMethodError) { object.success? }
|
9
|
+
|
10
|
+
object = Object.new
|
11
|
+
assert object.__metaclass__.ancestors.include?(Object)
|
12
|
+
assert object.__metaclass__.ancestors.include?(Kernel)
|
13
|
+
assert object.__metaclass__.is_a?(Class)
|
14
|
+
|
15
|
+
object.__metaclass__.class_eval { def success?; true; end }
|
16
|
+
assert object.success?
|
17
|
+
|
18
|
+
object = Object.new
|
19
|
+
assert_raises(NoMethodError) { object.success? }
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -49,7 +49,17 @@ class MockMethodsTest < Test::Unit::TestCase
|
|
49
49
|
expectation1 = mock.expects(:my_method).with(:argument1, :argument2)
|
50
50
|
expectation2 = mock.expects(:my_method).with(:argument3, :argument4)
|
51
51
|
|
52
|
-
|
52
|
+
assert_same expectation2, mock.matching_expectation(:my_method, :argument3, :argument4)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_should_find_most_recent_matching_expectation
|
56
|
+
mock = Object.new
|
57
|
+
mock.extend(MockMethods)
|
58
|
+
|
59
|
+
expectation1 = mock.expects(:my_method).with(:argument1, :argument2)
|
60
|
+
expectation2 = mock.expects(:my_method).with(:argument1, :argument2)
|
61
|
+
|
62
|
+
assert_same expectation2, mock.matching_expectation(:my_method, :argument1, :argument2)
|
53
63
|
end
|
54
64
|
|
55
65
|
def test_should_invoke_expectation_and_return_result
|
@@ -67,7 +77,7 @@ class MockMethodsTest < Test::Unit::TestCase
|
|
67
77
|
mock.extend(MockMethods)
|
68
78
|
|
69
79
|
result = nil
|
70
|
-
assert_nothing_raised(
|
80
|
+
assert_nothing_raised(ExpectationError) do
|
71
81
|
result = mock.unexpected_method
|
72
82
|
end
|
73
83
|
assert_nil result
|
@@ -84,7 +94,7 @@ class MockMethodsTest < Test::Unit::TestCase
|
|
84
94
|
def test_should_raise_assertion_error_for_unexpected_method_call
|
85
95
|
mock = Object.new
|
86
96
|
mock.extend(MockMethods)
|
87
|
-
error = assert_raise(
|
97
|
+
error = assert_raise(ExpectationError) do
|
88
98
|
mock.unexpected_method_called(:my_method, :argument1, :argument2)
|
89
99
|
end
|
90
100
|
assert_match /my_method/, error.message
|
@@ -129,16 +139,16 @@ class MockMethodsTest < Test::Unit::TestCase
|
|
129
139
|
mock.expects(:method1)
|
130
140
|
mock.expects(:method2)
|
131
141
|
mock.method1
|
132
|
-
assert_raise(
|
142
|
+
assert_raise(ExpectationError) do
|
133
143
|
mock.verify
|
134
144
|
end
|
135
145
|
end
|
136
146
|
|
137
147
|
def test_should_report_possible_expectations
|
138
148
|
mock = Object.new.extend(MockMethods)
|
139
|
-
mock.expects(:
|
140
|
-
exception = assert_raise(
|
141
|
-
assert_equal "
|
149
|
+
mock.expects(:expected_method).with(1)
|
150
|
+
exception = assert_raise(ExpectationError) { mock.expected_method(2) }
|
151
|
+
assert_equal "#{mock.mocha_inspect}.expected_method(2) - expected calls: 0, actual calls: 1\nSimilar expectations:\nexpected_method(1)", exception.message
|
142
152
|
end
|
143
153
|
|
144
154
|
def test_should_pass_block_through_to_expectations_verify_method
|
@@ -174,4 +184,52 @@ class MockMethodsTest < Test::Unit::TestCase
|
|
174
184
|
assert_equal false, mock.respond_to?(:method1)
|
175
185
|
end
|
176
186
|
|
187
|
+
def test_should_set_up_multiple_expectations_with_return_values
|
188
|
+
mock = Object.new
|
189
|
+
mock.extend(MockMethods)
|
190
|
+
mock.expects(:method1 => :result1, :method2 => :result2)
|
191
|
+
assert_equal :result1, mock.method1
|
192
|
+
assert_equal :result2, mock.method2
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_should_set_up_multiple_stubs_with_return_values
|
196
|
+
mock = Object.new
|
197
|
+
mock.extend(MockMethods)
|
198
|
+
mock.stubs(:method1 => :result1, :method2 => :result2)
|
199
|
+
assert_equal :result1, mock.method1
|
200
|
+
assert_equal :result2, mock.method2
|
201
|
+
end
|
202
|
+
|
203
|
+
def test_should_match_most_recent_call_to_expects
|
204
|
+
mock = Object.new
|
205
|
+
mock.extend(MockMethods)
|
206
|
+
mock.expects(:method1).returns(0)
|
207
|
+
mock.expects(:method1).returns(1)
|
208
|
+
assert_equal 1, mock.method1
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_should_match_most_recent_call_to_stubs
|
212
|
+
mock = Object.new
|
213
|
+
mock.extend(MockMethods)
|
214
|
+
mock.stubs(:method1).returns(0)
|
215
|
+
mock.stubs(:method1).returns(1)
|
216
|
+
assert_equal 1, mock.method1
|
217
|
+
end
|
218
|
+
|
219
|
+
def test_should_match_call_to_expects_with_previous_call_to_stubs
|
220
|
+
mock = Object.new
|
221
|
+
mock.extend(MockMethods)
|
222
|
+
mock.stubs(:method1).returns(0)
|
223
|
+
mock.expects(:method1).returns(1)
|
224
|
+
assert_equal 1, mock.method1
|
225
|
+
end
|
226
|
+
|
227
|
+
def test_should_match_call_to_stubs_with_previous_call_to_expects
|
228
|
+
mock = Object.new
|
229
|
+
mock.extend(MockMethods)
|
230
|
+
mock.expects(:method1).returns(0)
|
231
|
+
mock.stubs(:method1).returns(1)
|
232
|
+
assert_equal 1, mock.method1
|
233
|
+
end
|
234
|
+
|
177
235
|
end
|
data/test/mocha/mock_test.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
2
|
require 'mocha/mock'
|
3
|
+
require 'mocha/expectation_error'
|
3
4
|
|
4
5
|
class MockTest < Test::Unit::TestCase
|
5
6
|
|
@@ -12,7 +13,7 @@ class MockTest < Test::Unit::TestCase
|
|
12
13
|
def test_should_set_single_expectation
|
13
14
|
mock = Mock.new
|
14
15
|
mock.expects(:method1).returns(1)
|
15
|
-
assert_nothing_raised(
|
16
|
+
assert_nothing_raised(ExpectationError) do
|
16
17
|
assert_equal 1, mock.method1
|
17
18
|
end
|
18
19
|
end
|
@@ -34,14 +35,50 @@ class MockTest < Test::Unit::TestCase
|
|
34
35
|
assert_equal true, mock.stub_everything
|
35
36
|
end
|
36
37
|
|
37
|
-
def
|
38
|
+
def test_should_display_object_id_for_inspect_if_mock_has_no_name
|
38
39
|
mock = Mock.new
|
39
|
-
|
40
|
+
assert_match Regexp.new("#<Mock:0x[0-9A-Fa-f]{6}>"), mock.mocha_inspect
|
40
41
|
end
|
41
42
|
|
43
|
+
def test_should_display_name_for_inspect_if_mock_has_name
|
44
|
+
mock = Mock.new(false, 'named_mock')
|
45
|
+
assert_equal "#<Mock:named_mock>", mock.mocha_inspect
|
46
|
+
end
|
47
|
+
|
42
48
|
def test_should_give_name_in_inspect_message
|
43
49
|
mock = Mock.new(false, 'named_mock')
|
44
|
-
assert_equal "#<Mock:
|
50
|
+
assert_equal "#<Mock:named_mock>", mock.mocha_inspect
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_should_be_able_to_extend_mock_object_with_module
|
54
|
+
mock = Mock.new
|
55
|
+
assert_nothing_raised(ExpectationError) { mock.extend(Module.new) }
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_should_be_equal
|
59
|
+
mock = Mock.new
|
60
|
+
assert_equal true, mock.eql?(mock)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_should_be_able_to_mock_standard_object_methods
|
64
|
+
mock = Mock.new
|
65
|
+
object_methods = Object.public_instance_methods.reject { |m| m =~ /^__.*__$/ }.sort
|
66
|
+
object_methods.each { |method| mock.__expects__(method.to_sym).returns(method) }
|
67
|
+
object_methods.each { |method| assert_equal method, mock.__send__(method.to_sym) }
|
68
|
+
assert_nothing_raised(ExpectationError) { mock.verify }
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_should_be_able_to_stub_standard_object_methods
|
72
|
+
mock = Mock.new
|
73
|
+
object_methods = Object.public_instance_methods.reject { |m| m =~ /^__.*__$/ }.sort
|
74
|
+
object_methods.each { |method| mock.__stubs__(method.to_sym).returns(method) }
|
75
|
+
object_methods.each { |method| assert_equal method, mock.__send__(method.to_sym) }
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_should_respond_to_expected_methods
|
79
|
+
mock = Mock.new
|
80
|
+
mock.expects(:method1)
|
81
|
+
assert_equal true, mock.respond_to?(:method1)
|
45
82
|
end
|
46
83
|
|
47
84
|
end
|
@@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), "..", "test_helper")
|
|
2
2
|
require 'mocha/mock'
|
3
3
|
require 'method_definer'
|
4
4
|
|
5
|
-
require '
|
5
|
+
require 'mocha/object'
|
6
6
|
|
7
7
|
class ObjectTest < Test::Unit::TestCase
|
8
8
|
|
@@ -30,7 +30,7 @@ class ObjectTest < Test::Unit::TestCase
|
|
30
30
|
def test_should_stub_instance_method
|
31
31
|
instance = Object.new
|
32
32
|
$stubba = Mock.new
|
33
|
-
$stubba.expects(:stub).with(
|
33
|
+
$stubba.expects(:stub).with(Mocha::InstanceMethod.new(instance, :method1))
|
34
34
|
instance.expects(:method1)
|
35
35
|
$stubba.verify
|
36
36
|
end
|
@@ -48,7 +48,7 @@ class ObjectTest < Test::Unit::TestCase
|
|
48
48
|
$stubba = Mock.new
|
49
49
|
$stubba.stubs(:stub)
|
50
50
|
instance.expects(:method1).with(:value1, :value2)
|
51
|
-
assert_raise(
|
51
|
+
assert_raise(ExpectationError) { instance.verify }
|
52
52
|
end
|
53
53
|
|
54
54
|
def test_should_pass_backtrace_into_expects
|
@@ -96,7 +96,7 @@ class ObjectTest < Test::Unit::TestCase
|
|
96
96
|
def test_should_stub_class_method
|
97
97
|
klass = Class.new
|
98
98
|
$stubba = Mock.new
|
99
|
-
$stubba.expects(:stub).with(
|
99
|
+
$stubba.expects(:stub).with(Mocha::ClassMethod.new(klass, :method1))
|
100
100
|
klass.expects(:method1)
|
101
101
|
$stubba.verify
|
102
102
|
end
|
@@ -112,7 +112,7 @@ class ObjectTest < Test::Unit::TestCase
|
|
112
112
|
def test_should_stub_module_method
|
113
113
|
mod = Module.new
|
114
114
|
$stubba = Mock.new
|
115
|
-
$stubba.expects(:stub).with(
|
115
|
+
$stubba.expects(:stub).with(Mocha::ClassMethod.new(mod, :method1))
|
116
116
|
mod.expects(:method1)
|
117
117
|
$stubba.verify
|
118
118
|
end
|
@@ -126,19 +126,19 @@ class ObjectTest < Test::Unit::TestCase
|
|
126
126
|
end
|
127
127
|
|
128
128
|
def test_should_use_stubba_instance_method_for_object
|
129
|
-
assert_equal
|
129
|
+
assert_equal Mocha::InstanceMethod, Object.new.stubba_method
|
130
130
|
end
|
131
131
|
|
132
132
|
def test_should_use_stubba_class_method_for_module
|
133
|
-
assert_equal
|
133
|
+
assert_equal Mocha::ClassMethod, Module.new.stubba_method
|
134
134
|
end
|
135
135
|
|
136
136
|
def test_should_use_stubba_class_method_for_class
|
137
|
-
assert_equal
|
137
|
+
assert_equal Mocha::ClassMethod, Class.new.stubba_method
|
138
138
|
end
|
139
139
|
|
140
140
|
def test_should_use_stubba_class_method_for_any_instance
|
141
|
-
assert_equal
|
141
|
+
assert_equal Mocha::AnyInstanceMethod, Class::AnyInstance.new(nil).stubba_method
|
142
142
|
end
|
143
143
|
|
144
144
|
def test_should_stub_self_for_object
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
|
+
require 'mocha/mock'
|
3
|
+
|
4
|
+
require 'mocha/setup_and_teardown'
|
5
|
+
|
6
|
+
class SetupAndTeardownTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
include Mocha
|
9
|
+
|
10
|
+
def test_should_instantiate_new_stubba
|
11
|
+
test_case = stubbed_test_case_class.new
|
12
|
+
test_case.setup_stubs
|
13
|
+
|
14
|
+
assert $stubba
|
15
|
+
assert $stubba.is_a?(Mocha::Central)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_should_verify_all_expectations
|
19
|
+
test_case = stubbed_test_case_class.new
|
20
|
+
stubba = Mock.new
|
21
|
+
stubba.expects(:verify_all)
|
22
|
+
$stubba = stubba
|
23
|
+
|
24
|
+
test_case.verify_stubs
|
25
|
+
|
26
|
+
stubba.verify
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_should_yield_to_block_for_each_assertion
|
30
|
+
test_case = stubbed_test_case_class.new
|
31
|
+
$stubba = Mock.new
|
32
|
+
$stubba.stubs(:verify_all).yields
|
33
|
+
yielded = false
|
34
|
+
|
35
|
+
test_case.verify_stubs { yielded = true }
|
36
|
+
|
37
|
+
assert_equal true, yielded
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_should_unstub_all_stubbed_methods
|
41
|
+
test_case = stubbed_test_case_class.new
|
42
|
+
stubba = Mock.new
|
43
|
+
stubba.expects(:unstub_all)
|
44
|
+
$stubba = stubba
|
45
|
+
|
46
|
+
test_case.teardown_stubs
|
47
|
+
|
48
|
+
stubba.verify
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_should_set_stubba_to_nil
|
52
|
+
test_case = stubbed_test_case_class.new
|
53
|
+
$stubba = Mock.new
|
54
|
+
$stubba.stubs(:unstub_all)
|
55
|
+
|
56
|
+
test_case.teardown_stubs
|
57
|
+
|
58
|
+
assert_nil $stubba
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_should_not_raise_exception_if_no_stubba_central_available
|
62
|
+
test_case = stubbed_test_case_class.new
|
63
|
+
$stubba = nil
|
64
|
+
assert_nothing_raised { test_case.teardown_stubs }
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def stubbed_test_case_class
|
70
|
+
Class.new do
|
71
|
+
include Mocha::SetupAndTeardown
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
@@ -36,8 +36,8 @@ class MochaAcceptanceTest < Test::Unit::TestCase
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def test_should_step_both_tracks_forward_ten_steps
|
39
|
-
left_track = mock()
|
40
|
-
right_track = mock()
|
39
|
+
left_track = mock('left_track')
|
40
|
+
right_track = mock('right_track')
|
41
41
|
steps_per_metre = 5
|
42
42
|
rover = Rover.new(left_track, right_track, steps_per_metre, nil)
|
43
43
|
|
@@ -51,8 +51,8 @@ class MochaAcceptanceTest < Test::Unit::TestCase
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def test_should_step_both_tracks_backward_ten_steps
|
54
|
-
left_track = mock()
|
55
|
-
right_track = mock()
|
54
|
+
left_track = mock('left_track')
|
55
|
+
right_track = mock('right_track')
|
56
56
|
steps_per_metre = 5
|
57
57
|
rover = Rover.new(left_track, right_track, steps_per_metre, nil)
|
58
58
|
|
@@ -66,8 +66,8 @@ class MochaAcceptanceTest < Test::Unit::TestCase
|
|
66
66
|
end
|
67
67
|
|
68
68
|
def test_should_step_left_track_forwards_five_steps_and_right_track_backwards_five_steps
|
69
|
-
left_track = mock()
|
70
|
-
right_track = mock()
|
69
|
+
left_track = mock('left_track')
|
70
|
+
right_track = mock('right_track')
|
71
71
|
steps_per_degree = 5.0 / 90.0
|
72
72
|
rover = Rover.new(left_track, right_track, nil, steps_per_degree)
|
73
73
|
|
@@ -81,8 +81,8 @@ class MochaAcceptanceTest < Test::Unit::TestCase
|
|
81
81
|
end
|
82
82
|
|
83
83
|
def test_should_step_left_track_backwards_five_steps_and_right_track_forwards_five_steps
|
84
|
-
left_track = mock()
|
85
|
-
right_track = mock()
|
84
|
+
left_track = mock('left_track')
|
85
|
+
right_track = mock('right_track')
|
86
86
|
steps_per_degree = 5.0 / 90.0
|
87
87
|
rover = Rover.new(left_track, right_track, nil, steps_per_degree)
|
88
88
|
|
@@ -1,11 +1,10 @@
|
|
1
1
|
require 'test_helper'
|
2
|
-
require '
|
3
|
-
require 'mocha/
|
4
|
-
require 'shared/backtracefilter'
|
2
|
+
require 'mocha/standalone'
|
3
|
+
require 'mocha/test_case_adapter'
|
5
4
|
require 'execution_point'
|
6
5
|
|
7
6
|
class MochaTestResultIntegrationTest < Test::Unit::TestCase
|
8
|
-
|
7
|
+
|
9
8
|
def test_should_include_expectation_verification_in_assertion_count
|
10
9
|
test_result = run_test do
|
11
10
|
object = mock()
|
@@ -94,8 +93,8 @@ class MochaTestResultIntegrationTest < Test::Unit::TestCase
|
|
94
93
|
|
95
94
|
def run_test(test_result = Test::Unit::TestResult.new, &block)
|
96
95
|
test_class = Class.new(Test::Unit::TestCase) do
|
97
|
-
include
|
98
|
-
include Mocha::
|
96
|
+
include Mocha::Standalone
|
97
|
+
include Mocha::TestCaseAdapter
|
99
98
|
define_method(:test_me, &block)
|
100
99
|
end
|
101
100
|
test = test_class.new(:test_me)
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'mocha_standalone'
|
3
|
+
|
4
|
+
class NotATestUnitAssertionFailedError < StandardError
|
5
|
+
end
|
6
|
+
|
7
|
+
class NotATestUnitTestCase
|
8
|
+
|
9
|
+
include Mocha::Standalone
|
10
|
+
|
11
|
+
attr_reader :assertion_count
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@assertion_count = 0
|
15
|
+
end
|
16
|
+
|
17
|
+
def run(test_method)
|
18
|
+
mocha_setup
|
19
|
+
begin
|
20
|
+
prepare
|
21
|
+
begin
|
22
|
+
send(test_method)
|
23
|
+
mocha_verify { @assertion_count += 1 }
|
24
|
+
rescue Mocha::ExpectationError => e
|
25
|
+
new_error = NotATestUnitAssertionFailedError.new(e.message)
|
26
|
+
new_error.set_backtrace(e.backtrace)
|
27
|
+
raise new_error
|
28
|
+
ensure
|
29
|
+
cleanup
|
30
|
+
end
|
31
|
+
ensure
|
32
|
+
mocha_teardown
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def prepare
|
37
|
+
end
|
38
|
+
|
39
|
+
def cleanup
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
class SampleTest < NotATestUnitTestCase
|
45
|
+
|
46
|
+
def mocha_with_fulfilled_expectation
|
47
|
+
mockee = mock()
|
48
|
+
mockee.expects(:blah)
|
49
|
+
mockee.blah
|
50
|
+
end
|
51
|
+
|
52
|
+
def mocha_with_unfulfilled_expectation
|
53
|
+
mockee = mock()
|
54
|
+
mockee.expects(:blah)
|
55
|
+
end
|
56
|
+
|
57
|
+
def mocha_with_unexpected_invocation
|
58
|
+
mockee = mock()
|
59
|
+
mockee.blah
|
60
|
+
end
|
61
|
+
|
62
|
+
def stubba_with_fulfilled_expectation
|
63
|
+
stubbee = Class.new { define_method(:blah) {} }.new
|
64
|
+
stubbee.expects(:blah)
|
65
|
+
stubbee.blah
|
66
|
+
end
|
67
|
+
|
68
|
+
def stubba_with_unfulfilled_expectation
|
69
|
+
stubbee = Class.new { define_method(:blah) {} }.new
|
70
|
+
stubbee.expects(:blah)
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
require 'test/unit'
|
76
|
+
|
77
|
+
class StandaloneAcceptanceTest < Test::Unit::TestCase
|
78
|
+
|
79
|
+
attr_reader :sample_test
|
80
|
+
|
81
|
+
def setup
|
82
|
+
@sample_test = SampleTest.new
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_should_pass_mocha_test
|
86
|
+
assert_nothing_raised { sample_test.run(:mocha_with_fulfilled_expectation) }
|
87
|
+
assert_equal 1, sample_test.assertion_count
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_should_fail_mocha_test_due_to_unfulfilled_exception
|
91
|
+
assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:mocha_with_unfulfilled_expectation) }
|
92
|
+
assert_equal 1, sample_test.assertion_count
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_should_fail_mocha_test_due_to_unexpected_invocation
|
96
|
+
assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:mocha_with_unexpected_invocation) }
|
97
|
+
assert_equal 0, sample_test.assertion_count
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_should_pass_stubba_test
|
101
|
+
assert_nothing_raised { sample_test.run(:stubba_with_fulfilled_expectation) }
|
102
|
+
assert_equal 1, sample_test.assertion_count
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_should_fail_stubba_test
|
106
|
+
assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:stubba_with_unfulfilled_expectation) }
|
107
|
+
assert_equal 1, sample_test.assertion_count
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|