mocha 0.2.1 → 0.3.0
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 +6 -2
- data/RELEASE +11 -0
- data/Rakefile +1 -1
- data/TODO +1 -3
- data/lib/mocha.rb +1 -0
- data/lib/mocha/auto_verify.rb +18 -13
- data/lib/mocha/expectation.rb +18 -3
- data/lib/mocha/mock.rb +2 -19
- data/lib/mocha/mock_methods.rb +16 -13
- data/lib/shared/backtracefilter.rb +46 -0
- data/lib/smart_test_case/multiple_setup_and_teardown.rb +9 -1
- data/lib/stubba.rb +1 -0
- data/lib/stubba/any_instance_method.rb +9 -5
- data/lib/stubba/central.rb +8 -0
- data/lib/stubba/class_method.rb +15 -11
- data/lib/stubba/instance_method.rb +1 -1
- data/lib/stubba/object.rb +4 -4
- data/lib/stubba/setup_and_teardown.rb +8 -2
- data/test/all_tests.rb +4 -0
- data/test/execution_point.rb +34 -0
- data/test/mocha/auto_verify_test.rb +103 -3
- data/test/mocha/expectation_test.rb +64 -1
- data/test/mocha/mock_methods_test.rb +55 -19
- data/test/mocha/mock_test.rb +11 -38
- data/test/mocha_test_result_integration_test.rb +106 -0
- data/test/smart_test_case/multiple_setup_and_teardown_test.rb +132 -31
- data/test/stubba/any_instance_method_test.rb +14 -2
- data/test/stubba/central_test.rb +62 -2
- data/test/stubba/class_method_test.rb +19 -1
- data/test/stubba/object_test.rb +41 -8
- data/test/stubba/setup_and_teardown_test.rb +60 -2
- data/test/stubba_acceptance_test.rb +0 -5
- data/test/stubba_test_result_integration_test.rb +86 -0
- data/test/test_helper.rb +2 -2
- metadata +6 -2
data/lib/stubba/object.rb
CHANGED
@@ -24,17 +24,17 @@ class Object
|
|
24
24
|
def expects(symbol)
|
25
25
|
method = stubba_method.new(stubba_object, symbol)
|
26
26
|
$stubba.stub(method)
|
27
|
-
mocha.expects(symbol)
|
27
|
+
mocha.expects(symbol, caller)
|
28
28
|
end
|
29
29
|
|
30
30
|
def stubs(symbol)
|
31
31
|
method = stubba_method.new(stubba_object, symbol)
|
32
32
|
$stubba.stub(method)
|
33
|
-
mocha.stubs(symbol)
|
33
|
+
mocha.stubs(symbol, caller)
|
34
34
|
end
|
35
35
|
|
36
|
-
def verify
|
37
|
-
mocha.verify
|
36
|
+
def verify
|
37
|
+
mocha.verify
|
38
38
|
end
|
39
39
|
|
40
40
|
end
|
@@ -12,8 +12,14 @@ module SetupAndTeardown
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def teardown_stubs
|
15
|
-
|
16
|
-
|
15
|
+
if $stubba then
|
16
|
+
begin
|
17
|
+
$stubba.verify_all { add_assertion }
|
18
|
+
ensure
|
19
|
+
$stubba.unstub_all
|
20
|
+
$stubba = nil
|
21
|
+
end
|
22
|
+
end
|
17
23
|
end
|
18
24
|
|
19
25
|
end
|
data/test/all_tests.rb
CHANGED
@@ -47,12 +47,16 @@ end
|
|
47
47
|
|
48
48
|
Test::Unit::UI::Console::TestRunner.run(UnitTests)
|
49
49
|
|
50
|
+
require 'mocha_test_result_integration_test'
|
51
|
+
require 'stubba_test_result_integration_test'
|
50
52
|
require 'stubba_integration_test'
|
51
53
|
|
52
54
|
class IntegrationTests
|
53
55
|
|
54
56
|
def self.suite
|
55
57
|
suite = Test::Unit::TestSuite.new('IntegrationTests')
|
58
|
+
suite << MochaTestResultIntegrationTest.suite
|
59
|
+
suite << StubbaTestResultIntegrationTest.suite
|
56
60
|
suite << StubbaIntegrationTest.suite
|
57
61
|
end
|
58
62
|
|
@@ -0,0 +1,34 @@
|
|
1
|
+
class ExecutionPoint
|
2
|
+
|
3
|
+
attr_reader :backtrace
|
4
|
+
|
5
|
+
def self.current
|
6
|
+
new(caller)
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(backtrace)
|
10
|
+
@backtrace = backtrace
|
11
|
+
end
|
12
|
+
|
13
|
+
def file_name
|
14
|
+
/\A(.*?):\d+/.match(@backtrace.first)[1]
|
15
|
+
end
|
16
|
+
|
17
|
+
def line_number
|
18
|
+
Integer(/\A.*?:(\d+)/.match(@backtrace.first)[1])
|
19
|
+
end
|
20
|
+
|
21
|
+
def ==(other)
|
22
|
+
return false unless other.is_a?(ExecutionPoint)
|
23
|
+
(file_name == other.file_name) and (line_number == other.line_number)
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_s
|
27
|
+
"file: #{file_name} line: #{line_number}"
|
28
|
+
end
|
29
|
+
|
30
|
+
def inspect
|
31
|
+
to_s
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -14,16 +14,84 @@ class AutoVerifyTest < Test::Unit::TestCase
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
+
def test_should_add_mock_type_expectations
|
18
|
+
test_case.define_instance_accessor(:expectation_type, :expectations)
|
19
|
+
test_case.define_instance_method(:build_mock_with_expectations) do |expectation_type, expectations|
|
20
|
+
self.expectation_type = expectation_type
|
21
|
+
self.expectations = expectations
|
22
|
+
end
|
23
|
+
expectations = { :method1 => :value1, :method2 => :value2 }
|
24
|
+
|
25
|
+
test_case.mock(expectations)
|
26
|
+
|
27
|
+
assert_equal :expects, test_case.expectation_type
|
28
|
+
assert_equal expectations, test_case.expectations
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_should_add_stub_type_expectations
|
32
|
+
test_case.define_instance_accessor(:expectation_type, :expectations)
|
33
|
+
test_case.define_instance_method(:build_mock_with_expectations) do |expectation_type, expectations|
|
34
|
+
self.expectation_type = expectation_type
|
35
|
+
self.expectations = expectations
|
36
|
+
end
|
37
|
+
expectations = { :method1 => :value1, :method2 => :value2 }
|
38
|
+
|
39
|
+
test_case.stub(expectations)
|
40
|
+
|
41
|
+
assert_equal :stubs, test_case.expectation_type
|
42
|
+
assert_equal expectations, test_case.expectations
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_should_add_greedy_stub_type_expectations
|
46
|
+
test_case.define_instance_accessor(:expectation_type, :expectations)
|
47
|
+
test_case.define_instance_method(:build_mock_with_expectations) do |expectation_type, expectations|
|
48
|
+
self.expectation_type = expectation_type
|
49
|
+
self.expectations = expectations
|
50
|
+
end
|
51
|
+
expectations = { :method1 => :value1, :method2 => :value2 }
|
52
|
+
|
53
|
+
test_case.stub_everything(expectations)
|
54
|
+
|
55
|
+
assert_equal :stub_everything, test_case.expectation_type
|
56
|
+
assert_equal expectations, test_case.expectations
|
57
|
+
end
|
58
|
+
|
17
59
|
def test_should_build_mock
|
18
|
-
|
60
|
+
mock = test_case.build_mock_with_expectations
|
61
|
+
assert mock.is_a?(Mocha::Mock)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_should_add_expectation_to_mock
|
65
|
+
mock = test_case.build_mock_with_expectations(:expects, :expected_method => 'return_value')
|
66
|
+
assert_equal 'return_value', mock.expected_method
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_should_build_stub
|
70
|
+
stub = test_case.build_mock_with_expectations(:stubs)
|
71
|
+
assert stub.is_a?(Mocha::Mock)
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_should_add_expectation_to_stub
|
75
|
+
stub = test_case.build_mock_with_expectations(:stubs, :stubbed_method => 'return_value')
|
76
|
+
assert_equal 'return_value', stub.stubbed_method
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_should_build_greedy_stub
|
80
|
+
greedy_stub = test_case.build_mock_with_expectations(:stub_everything)
|
81
|
+
assert greedy_stub.stub_everything
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_should_add_expectations_to_greedy_stub
|
85
|
+
greedy_mock = test_case.build_mock_with_expectations(:stub_everything, :stubbed_method => 'return_value')
|
86
|
+
assert_equal 'return_value', greedy_mock.stubbed_method
|
19
87
|
end
|
20
88
|
|
21
89
|
def test_should_build_new_mock_each_time
|
22
|
-
assert_not_equal test_case.
|
90
|
+
assert_not_equal test_case.build_mock_with_expectations, test_case.build_mock_with_expectations
|
23
91
|
end
|
24
92
|
|
25
93
|
def test_should_store_each_new_mock
|
26
|
-
expected = Array.new(3) { test_case.
|
94
|
+
expected = Array.new(3) { test_case.build_mock_with_expectations }
|
27
95
|
assert_equal expected, test_case.mocks
|
28
96
|
end
|
29
97
|
|
@@ -39,6 +107,18 @@ class AutoVerifyTest < Test::Unit::TestCase
|
|
39
107
|
assert mocks.all? { |mock| mock.verify_called }
|
40
108
|
end
|
41
109
|
|
110
|
+
def test_should_provide_block_for_adding_assertion
|
111
|
+
mock_class = Class.new do
|
112
|
+
def verify(&block); yield; end
|
113
|
+
end
|
114
|
+
mock = mock_class.new
|
115
|
+
test_case.replace_instance_method(:mocks) { [mock] }
|
116
|
+
test_case.define_instance_accessor(:add_assertion_called)
|
117
|
+
test_case.define_instance_method(:add_assertion) { self.add_assertion_called = true }
|
118
|
+
test_case.teardown_mocks
|
119
|
+
assert test_case.add_assertion_called
|
120
|
+
end
|
121
|
+
|
42
122
|
def test_should_reset_mocks_on_teardown
|
43
123
|
mock = Class.new { define_method(:verify) {} }.new
|
44
124
|
test_case.mocks << mock
|
@@ -46,4 +126,24 @@ class AutoVerifyTest < Test::Unit::TestCase
|
|
46
126
|
assert test_case.mocks.empty?
|
47
127
|
end
|
48
128
|
|
129
|
+
def test_should_stub_everything
|
130
|
+
mock = test_case.stub_everything
|
131
|
+
assert_equal true, mock.stub_everything
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_should_add_mock_to_mocks
|
135
|
+
mock = test_case.mock
|
136
|
+
assert_equal [mock], test_case.mocks
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_should_add_stub_to_mocks
|
140
|
+
stub = test_case.stub
|
141
|
+
assert_equal [stub], test_case.mocks
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_should_add_greedy_stub_to_mocks
|
145
|
+
greedy_stub = test_case.stub_everything
|
146
|
+
assert_equal [greedy_stub], test_case.mocks
|
147
|
+
end
|
148
|
+
|
49
149
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
2
|
require 'method_definer'
|
3
|
-
|
4
3
|
require 'mocha/expectation'
|
4
|
+
require 'execution_point'
|
5
5
|
|
6
6
|
class ExpectationTest < Test::Unit::TestCase
|
7
7
|
|
@@ -57,6 +57,39 @@ class ExpectationTest < Test::Unit::TestCase
|
|
57
57
|
assert !expectation.match?(:expected_method, 1, 0, 3)
|
58
58
|
end
|
59
59
|
|
60
|
+
def test_should_store_provided_backtrace
|
61
|
+
backtrace = Object.new
|
62
|
+
expectation = Expectation.new(:expected_method, backtrace)
|
63
|
+
assert_equal backtrace, expectation.backtrace
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_should_default_backtrace_to_caller
|
67
|
+
execution_point = ExecutionPoint.current; expectation = Expectation.new(:expected_method)
|
68
|
+
assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_should_not_yield
|
72
|
+
expectation = Expectation.new(:expected_method)
|
73
|
+
yielded = false
|
74
|
+
expectation.invoke() { yielded = true }
|
75
|
+
assert_equal false, yielded
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_should_yield_no_parameters
|
79
|
+
expectation = Expectation.new(:expected_method).yields
|
80
|
+
yielded_parameters = nil
|
81
|
+
expectation.invoke() { |*parameters| yielded_parameters = parameters }
|
82
|
+
assert_equal Array.new, yielded_parameters
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_should_yield_with_specified_parameters
|
86
|
+
parameters_for_yield = [1, 2, 3]
|
87
|
+
expectation = Expectation.new(:expected_method).yields(*parameters_for_yield)
|
88
|
+
yielded_parameters = nil
|
89
|
+
expectation.invoke() { |*parameters| yielded_parameters = parameters }
|
90
|
+
assert_equal parameters_for_yield, yielded_parameters
|
91
|
+
end
|
92
|
+
|
60
93
|
def test_should_return_specified_value
|
61
94
|
expectation = Expectation.new(:expected_method).returns(99)
|
62
95
|
assert_equal 99, expectation.invoke
|
@@ -153,6 +186,36 @@ class ExpectationTest < Test::Unit::TestCase
|
|
153
186
|
}
|
154
187
|
end
|
155
188
|
|
189
|
+
def test_should_yield_self_to_block
|
190
|
+
expectation = Expectation.new(:expected_method)
|
191
|
+
expectation.invoke
|
192
|
+
yielded_expectation = nil
|
193
|
+
expectation.verify { |x| yielded_expectation = x }
|
194
|
+
assert_equal expectation, yielded_expectation
|
195
|
+
end
|
196
|
+
|
197
|
+
def test_should_yield_to_block_before_raising_exception
|
198
|
+
expectation = Expectation.new(:expected_method)
|
199
|
+
yielded = false
|
200
|
+
assert_raise(Test::Unit::AssertionFailedError) {
|
201
|
+
expectation.verify { |x| yielded = true }
|
202
|
+
}
|
203
|
+
assert yielded
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_should_store_backtrace_from_point_where_expectation_was_created
|
207
|
+
execution_point = ExecutionPoint.current; expectation = Expectation.new(:expected_method)
|
208
|
+
assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_should_set_backtrace_on_assertion_failed_error_to_point_where_expectation_was_created
|
212
|
+
execution_point = ExecutionPoint.current; expectation = Expectation.new(:expected_method)
|
213
|
+
error = assert_raise(Test::Unit::AssertionFailedError) {
|
214
|
+
expectation.verify
|
215
|
+
}
|
216
|
+
assert_equal execution_point, ExecutionPoint.new(error.backtrace)
|
217
|
+
end
|
218
|
+
|
156
219
|
def test_should_display_expectation_message_in_exception_message
|
157
220
|
options = [:a, :b, {:c => 1, :d => 2}]
|
158
221
|
expectation = Expectation.new(:expected_method).with(*options)
|
@@ -16,6 +16,22 @@ class MockMethodsTest < Test::Unit::TestCase
|
|
16
16
|
assert_equal [expectation1, expectation2].to_set, mock.expectations.to_set
|
17
17
|
end
|
18
18
|
|
19
|
+
def test_should_pass_backtrace_into_expectation
|
20
|
+
mock = Object.new
|
21
|
+
mock.extend(MockMethods)
|
22
|
+
backtrace = Object.new
|
23
|
+
expectation = mock.expects(:method1, backtrace)
|
24
|
+
assert_equal backtrace, expectation.backtrace
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_should_pass_backtrace_into_stub
|
28
|
+
mock = Object.new
|
29
|
+
mock.extend(MockMethods)
|
30
|
+
backtrace = Object.new
|
31
|
+
stub = mock.stubs(:method1, backtrace)
|
32
|
+
assert_equal backtrace, stub.backtrace
|
33
|
+
end
|
34
|
+
|
19
35
|
def test_should_create_and_add_stubs
|
20
36
|
mock = Object.new
|
21
37
|
mock.extend(MockMethods)
|
@@ -46,6 +62,17 @@ class MockMethodsTest < Test::Unit::TestCase
|
|
46
62
|
assert_equal :result, result
|
47
63
|
end
|
48
64
|
|
65
|
+
def test_should_not_raise_error_if_stubbing_everything
|
66
|
+
mock = Class.new { def initialize; @stub_everything = true; end }.new
|
67
|
+
mock.extend(MockMethods)
|
68
|
+
|
69
|
+
result = nil
|
70
|
+
assert_nothing_raised(Test::Unit::AssertionFailedError) do
|
71
|
+
result = mock.unexpected_method
|
72
|
+
end
|
73
|
+
assert_nil result
|
74
|
+
end
|
75
|
+
|
49
76
|
def test_should_raise_no_method_error
|
50
77
|
mock = Object.new
|
51
78
|
mock.extend(MockMethods)
|
@@ -107,35 +134,44 @@ class MockMethodsTest < Test::Unit::TestCase
|
|
107
134
|
end
|
108
135
|
end
|
109
136
|
|
110
|
-
def
|
137
|
+
def test_should_report_possible_expectations
|
138
|
+
mock = Object.new.extend(MockMethods)
|
139
|
+
mock.expects(:meth).with(1)
|
140
|
+
exception = assert_raise(Test::Unit::AssertionFailedError) { mock.meth(2) }
|
141
|
+
assert_equal "Unexpected message :meth(2)\nSimilar expectations :meth(1)", exception.message
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_should_pass_block_through_to_expectations_verify_method
|
111
145
|
mock = Object.new
|
112
146
|
mock.extend(MockMethods)
|
113
|
-
mock.expects(:method1)
|
114
|
-
mock.expects(:method2)
|
147
|
+
expected_expectation = mock.expects(:method1)
|
115
148
|
mock.method1
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
149
|
+
expectations = []
|
150
|
+
mock.verify() { |expectation| expectations << expectation }
|
151
|
+
assert_equal [expected_expectation], expectations
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_should_yield_supplied_parameters_to_block
|
155
|
+
mock = Object.new
|
156
|
+
mock.extend(MockMethods)
|
157
|
+
parameters_for_yield = [1, 2, 3]
|
158
|
+
mock.expects(:method1).yields(*parameters_for_yield)
|
159
|
+
yielded_parameters = nil
|
160
|
+
mock.method1() { |*parameters| yielded_parameters = parameters }
|
161
|
+
assert_equal parameters_for_yield, yielded_parameters
|
122
162
|
end
|
123
163
|
|
124
|
-
def
|
164
|
+
def test_should_respond_to_expected_method
|
125
165
|
mock = Object.new
|
126
166
|
mock.extend(MockMethods)
|
127
167
|
mock.expects(:method1)
|
128
|
-
mock.
|
129
|
-
assert_raise(Test::Unit::AssertionFailedError) do
|
130
|
-
mock.verify(:method1, :method2)
|
131
|
-
end
|
168
|
+
assert_equal true, mock.respond_to?(:method1)
|
132
169
|
end
|
133
170
|
|
134
|
-
def
|
135
|
-
mock = Object.new
|
136
|
-
mock.
|
137
|
-
|
138
|
-
assert_equal "Unexpected message :meth(2)\nSimilar expectations :meth(1)", exception.message
|
171
|
+
def test_should_not_respond_to_unexpected_method
|
172
|
+
mock = Object.new
|
173
|
+
mock.extend(MockMethods)
|
174
|
+
assert_equal false, mock.respond_to?(:method1)
|
139
175
|
end
|
140
176
|
|
141
177
|
end
|
data/test/mocha/mock_test.rb
CHANGED
@@ -17,48 +17,21 @@ class MockTest < Test::Unit::TestCase
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
def test_should_set_multiple_expectations_in_constructor
|
21
|
-
mock = Mock.new(:method1 => 1, :method2 => 2)
|
22
|
-
assert_nothing_raised(Test::Unit::AssertionFailedError) do
|
23
|
-
assert_equal 1, mock.method1
|
24
|
-
assert_equal 2, mock.method2
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_should_claim_to_respond_to_any_method
|
29
|
-
mock = Mock.new
|
30
|
-
always_responds = mock.always_responds
|
31
|
-
assert always_responds.respond_to?(:gobble_de_gook)
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_should_raise_exception_if_attempting_to_mock_undefined_method
|
35
|
-
mocked = Class.new
|
36
|
-
mock = Mock.new(mocked.new)
|
37
|
-
assert_raise(Test::Unit::AssertionFailedError) do
|
38
|
-
mock.expects(:method1)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_should_not_raise_exception_if_attempting_to_mock_defined_method
|
43
|
-
mocked = Class.new { def method1; end }
|
44
|
-
mock = Mock.new(mocked.new)
|
45
|
-
assert_nothing_raised(Test::Unit::AssertionFailedError) do
|
46
|
-
mock.expects(:method1)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_should_not_raise_exception_if_attempting_to_mock_method_when_no_class_specified
|
51
|
-
mock = Mock.new
|
52
|
-
assert_nothing_raised(Test::Unit::AssertionFailedError) do
|
53
|
-
mock.expects(:method1)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
20
|
def test_should_build_and_store_expectations
|
58
21
|
mock = Mock.new
|
59
22
|
expectation = mock.expects(:method1)
|
60
23
|
assert_not_nil expectation
|
61
24
|
assert_equal [expectation], mock.expectations
|
62
25
|
end
|
63
|
-
|
26
|
+
|
27
|
+
def test_should_not_stub_everything_by_default
|
28
|
+
mock = Mock.new
|
29
|
+
assert_equal false, mock.stub_everything
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_stub_everything
|
33
|
+
mock = Mock.new(stub_everything = true)
|
34
|
+
assert_equal true, mock.stub_everything
|
35
|
+
end
|
36
|
+
|
64
37
|
end
|