flexmock 0.6.4 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +8 -0
- data/README +116 -3
- data/Rakefile +5 -5
- data/TAGS +396 -282
- data/doc/releases/flexmock-0.7.0.rdoc +138 -0
- data/lib/flexmock/base.rb +2 -1
- data/lib/flexmock/core.rb +30 -29
- data/lib/flexmock/core_class_methods.rb +21 -30
- data/lib/flexmock/deprecated_methods.rb +62 -0
- data/lib/flexmock/expectation.rb +101 -21
- data/lib/flexmock/expectation_director.rb +10 -5
- data/lib/flexmock/mock_container.rb +144 -29
- data/lib/flexmock/ordering.rb +51 -0
- data/lib/flexmock/partial_mock.rb +23 -22
- data/test/asserts.rb +34 -0
- data/test/redirect_error.rb +16 -0
- data/test/rspec_integration/integration_spec.rb +6 -0
- data/test/test_demeter_mocking.rb +129 -0
- data/test/{test_mock.rb → test_deprecated_methods.rb} +64 -17
- data/test/test_example.rb +4 -3
- data/test/test_extended_should_receive.rb +1 -1
- data/test/test_flexmodel.rb +17 -1
- data/test/test_naming.rb +43 -8
- data/test/test_new_instances.rb +2 -22
- data/test/test_partial_mock.rb +19 -19
- data/test/test_record_mode.rb +27 -38
- data/test/test_should_receive.rb +199 -27
- metadata +11 -4
data/test/test_new_instances.rb
CHANGED
@@ -11,9 +11,11 @@
|
|
11
11
|
|
12
12
|
require 'test/unit'
|
13
13
|
require 'flexmock'
|
14
|
+
require 'test/redirect_error'
|
14
15
|
|
15
16
|
class TestNewInstances < Test::Unit::TestCase
|
16
17
|
include FlexMock::TestCase
|
18
|
+
include FlexMock::RedirectError
|
17
19
|
|
18
20
|
class Dog
|
19
21
|
def bark
|
@@ -55,17 +57,6 @@ class TestNewInstances < Test::Unit::TestCase
|
|
55
57
|
assert_equal :whimper, m.bark
|
56
58
|
end
|
57
59
|
|
58
|
-
def test_any_instance_still_works_for_backwards_compatibility
|
59
|
-
message = redirect_error {
|
60
|
-
flexstub(Dog).any_instance do |obj|
|
61
|
-
obj.should_receive(:bark).and_return(:whimper)
|
62
|
-
assert_match(/deprecated/, message)
|
63
|
-
end
|
64
|
-
}
|
65
|
-
m = Dog.new
|
66
|
-
assert_equal :whimper, m.bark
|
67
|
-
end
|
68
|
-
|
69
60
|
def test_new_instances_stubs_still_have_existing_methods
|
70
61
|
flexstub(Dog).new_instances do |obj|
|
71
62
|
obj.should_receive(:bark).and_return(:whimper)
|
@@ -214,17 +205,6 @@ class TestNewInstances < Test::Unit::TestCase
|
|
214
205
|
dog.bite
|
215
206
|
dog.bark
|
216
207
|
end
|
217
|
-
|
218
|
-
def redirect_error
|
219
|
-
require 'stringio'
|
220
|
-
old_err = $stderr
|
221
|
-
$stderr = StringIO.new
|
222
|
-
yield
|
223
|
-
$stderr.string
|
224
|
-
rescue
|
225
|
-
$stderr = old_err
|
226
|
-
end
|
227
|
-
private :redirect_error
|
228
208
|
end
|
229
209
|
|
230
210
|
|
data/test/test_partial_mock.rb
CHANGED
@@ -64,7 +64,7 @@ class TestStubbing < Test::Unit::TestCase
|
|
64
64
|
partial_mock = flexmock(dog)
|
65
65
|
partial_mock.should_receive(:bark).once.and_return(:growl)
|
66
66
|
assert_equal :growl, dog.bark
|
67
|
-
partial_mock.
|
67
|
+
partial_mock.flexmock_teardown
|
68
68
|
assert_equal :woof, dog.bark
|
69
69
|
assert_equal nil, dog.instance_variable_get("@flexmock_proxy")
|
70
70
|
end
|
@@ -74,7 +74,7 @@ class TestStubbing < Test::Unit::TestCase
|
|
74
74
|
partial_mock = flexmock(obj)
|
75
75
|
partial_mock.should_receive(:hi).once.and_return(:ok)
|
76
76
|
assert_equal :ok, obj.hi
|
77
|
-
partial_mock.
|
77
|
+
partial_mock.flexmock_teardown
|
78
78
|
assert_raise(NoMethodError) { obj.hi }
|
79
79
|
end
|
80
80
|
|
@@ -85,7 +85,7 @@ class TestStubbing < Test::Unit::TestCase
|
|
85
85
|
partial_mock.should_receive(:hi).with(2).once.and_return(:ok)
|
86
86
|
assert_equal :ok, obj.hi(1)
|
87
87
|
assert_equal :ok, obj.hi(2)
|
88
|
-
partial_mock.
|
88
|
+
partial_mock.flexmock_teardown
|
89
89
|
assert_raise(NoMethodError) { obj.hi }
|
90
90
|
end
|
91
91
|
|
@@ -93,7 +93,7 @@ class TestStubbing < Test::Unit::TestCase
|
|
93
93
|
dog = Dog.new
|
94
94
|
flexmock(dog).should_receive(:bark).and_return(:grrrr)
|
95
95
|
flexmock(dog).should_receive(:wag).and_return(:happy)
|
96
|
-
flexmock(dog).
|
96
|
+
flexmock(dog).flexmock_teardown
|
97
97
|
assert_equal :woof, dog.bark
|
98
98
|
assert_raise(NoMethodError) { dog.wag }
|
99
99
|
end
|
@@ -101,7 +101,7 @@ class TestStubbing < Test::Unit::TestCase
|
|
101
101
|
def test_original_behavior_is_restored_on_class_objects
|
102
102
|
flexmock(Dog).should_receive(:create).once.and_return(:new_stub)
|
103
103
|
assert_equal :new_stub, Dog.create
|
104
|
-
flexmock(Dog).
|
104
|
+
flexmock(Dog).flexmock_teardown
|
105
105
|
assert_equal :new_dog, Dog.create
|
106
106
|
end
|
107
107
|
|
@@ -111,7 +111,7 @@ class TestStubbing < Test::Unit::TestCase
|
|
111
111
|
flexmock(obj).should_receive(:hi).once.and_return(:hola)
|
112
112
|
|
113
113
|
assert_equal :hola, obj.hi
|
114
|
-
flexmock(obj).
|
114
|
+
flexmock(obj).flexmock_teardown
|
115
115
|
assert_equal :hello, obj.hi
|
116
116
|
end
|
117
117
|
|
@@ -123,7 +123,7 @@ class TestStubbing < Test::Unit::TestCase
|
|
123
123
|
|
124
124
|
assert_equal :hola, obj.hi(1)
|
125
125
|
assert_equal :hola, obj.hi(2)
|
126
|
-
flexmock(obj).
|
126
|
+
flexmock(obj).flexmock_teardown
|
127
127
|
assert_equal "hello3", obj.hi(3)
|
128
128
|
end
|
129
129
|
|
@@ -133,7 +133,7 @@ class TestStubbing < Test::Unit::TestCase
|
|
133
133
|
assert_equal :ok1, Dir.chdir("xx")
|
134
134
|
assert_equal :ok2, Dir.chdir("yy")
|
135
135
|
|
136
|
-
flexmock(Dir).
|
136
|
+
flexmock(Dir).flexmock_teardown
|
137
137
|
|
138
138
|
x = :not_called
|
139
139
|
Dir.chdir("test") do
|
@@ -146,7 +146,7 @@ class TestStubbing < Test::Unit::TestCase
|
|
146
146
|
def test_stubbing_file_shouldnt_break_writing
|
147
147
|
flexmock(File).should_receive(:open).with("foo").once.and_return(:ok)
|
148
148
|
assert_equal :ok, File.open("foo")
|
149
|
-
flexmock(File).
|
149
|
+
flexmock(File).flexmock_teardown
|
150
150
|
|
151
151
|
File.open("dummy.txt", "w") do |out|
|
152
152
|
assert out.is_a?(IO)
|
@@ -166,15 +166,15 @@ class TestStubbing < Test::Unit::TestCase
|
|
166
166
|
|
167
167
|
# Now disable the mock so that it doesn't cause errors on normal
|
168
168
|
# test teardown
|
169
|
-
m = flexmock(Dog).
|
170
|
-
def m.
|
169
|
+
m = flexmock(Dog).flexmock_get
|
170
|
+
def m.flexmock_verify() end
|
171
171
|
end
|
172
172
|
|
173
173
|
def test_not_calling_stubbed_method_is_an_error
|
174
174
|
dog = Dog.new
|
175
175
|
flexmock(dog).should_receive(:bark).once
|
176
176
|
assert_raise(Test::Unit::AssertionFailedError) {
|
177
|
-
flexmock(dog).
|
177
|
+
flexmock(dog).flexmock_verify
|
178
178
|
}
|
179
179
|
dog.bark
|
180
180
|
end
|
@@ -184,20 +184,20 @@ class TestStubbing < Test::Unit::TestCase
|
|
184
184
|
partial_mock = flexmock(obj)
|
185
185
|
partial_mock.should_receive(:hi).once.and_return(:ok)
|
186
186
|
assert_raise(Test::Unit::AssertionFailedError) {
|
187
|
-
partial_mock.
|
187
|
+
partial_mock.flexmock_verify
|
188
188
|
}
|
189
189
|
end
|
190
190
|
|
191
191
|
def test_stub_can_have_explicit_name
|
192
192
|
obj = Object.new
|
193
193
|
partial_mock = flexmock(obj, "Charlie")
|
194
|
-
assert_equal "Charlie", partial_mock.
|
194
|
+
assert_equal "Charlie", partial_mock.flexmock_get.flexmock_name
|
195
195
|
end
|
196
196
|
|
197
197
|
def test_unamed_stub_will_use_default_naming_convention
|
198
198
|
obj = Object.new
|
199
199
|
partial_mock = flexmock(obj)
|
200
|
-
assert_equal "flexmock(Object)", partial_mock.
|
200
|
+
assert_equal "flexmock(Object)", partial_mock.flexmock_get.flexmock_name
|
201
201
|
end
|
202
202
|
|
203
203
|
def test_partials_can_be_defined_in_a_block
|
@@ -223,7 +223,7 @@ class TestStubbing < Test::Unit::TestCase
|
|
223
223
|
|
224
224
|
MOCK_METHOD_SUBSET = [
|
225
225
|
:should_receive, :new_instances,
|
226
|
-
:
|
226
|
+
:flexmock_get, :flexmock_teardown, :flexmock_verify,
|
227
227
|
]
|
228
228
|
|
229
229
|
def test_domain_objects_do_not_have_mock_methods
|
@@ -244,7 +244,7 @@ class TestStubbing < Test::Unit::TestCase
|
|
244
244
|
def test_partial_mocks_do_not_have_mock_methods_after_teardown
|
245
245
|
dog = Dog.new
|
246
246
|
flexmock(dog)
|
247
|
-
dog.
|
247
|
+
dog.flexmock_teardown
|
248
248
|
MOCK_METHOD_SUBSET.each do |sym|
|
249
249
|
assert ! dog.respond_to?(sym), "should not have :#{sym} defined"
|
250
250
|
end
|
@@ -254,7 +254,7 @@ class TestStubbing < Test::Unit::TestCase
|
|
254
254
|
dog = Dog.new
|
255
255
|
def dog.mock() :original end
|
256
256
|
flexmock(dog)
|
257
|
-
dog.
|
257
|
+
dog.flexmock_teardown
|
258
258
|
assert_equal :original, dog.mock
|
259
259
|
end
|
260
260
|
|
@@ -267,7 +267,7 @@ class TestStubbing < Test::Unit::TestCase
|
|
267
267
|
def test_partial_mocks_with_mock_method_non_singleton_colision_have_original_defs_restored
|
268
268
|
mc = MockColision.new
|
269
269
|
flexmock(mc)
|
270
|
-
mc.
|
270
|
+
mc.flexmock_teardown
|
271
271
|
assert_equal :original, mc.mock
|
272
272
|
end
|
273
273
|
|
data/test/test_record_mode.rb
CHANGED
@@ -11,39 +11,29 @@
|
|
11
11
|
|
12
12
|
require 'test/unit'
|
13
13
|
require 'flexmock'
|
14
|
-
|
15
|
-
module FailureAssertion
|
16
|
-
private
|
17
|
-
def assert_fails
|
18
|
-
assert_raise(Test::Unit::AssertionFailedError) do
|
19
|
-
yield
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
14
|
+
require 'test/asserts'
|
23
15
|
|
24
16
|
class TestRecordMode < Test::Unit::TestCase
|
25
|
-
include FlexMock::
|
26
|
-
include FailureAssertion
|
17
|
+
include FlexMock::TestCase
|
18
|
+
include FlexMock::FailureAssertion
|
27
19
|
|
28
20
|
def test_recording_mode_works
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
end
|
33
|
-
assert_equal :answer, mock.f
|
21
|
+
mock = flexmock("mock")
|
22
|
+
mock.should_expect do |recorder|
|
23
|
+
recorder.f { :answer }
|
34
24
|
end
|
25
|
+
assert_equal :answer, mock.f
|
35
26
|
end
|
36
27
|
|
37
28
|
def test_arguments_are_passed_to_recording_mode_block
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
29
|
+
mock = flexmock("mock")
|
30
|
+
mock.should_expect do |recorder|
|
31
|
+
recorder.f(:arg) do |arg|
|
32
|
+
assert_equal :arg, arg
|
33
|
+
:answer
|
44
34
|
end
|
45
|
-
assert_equal :answer, f(:arg)
|
46
35
|
end
|
36
|
+
assert_equal :answer, mock.f(:arg)
|
47
37
|
end
|
48
38
|
|
49
39
|
def test_recording_mode_handles_multiple_returns
|
@@ -69,20 +59,19 @@ class TestRecordMode < Test::Unit::TestCase
|
|
69
59
|
end
|
70
60
|
|
71
61
|
def test_recording_mode_gets_block_args_too
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
end
|
79
|
-
|
80
|
-
assert_equal :block_result, mock.f(1) { :block_result }
|
62
|
+
mock = flexmock("mock")
|
63
|
+
mock.should_expect do |r|
|
64
|
+
r.f(1, Proc) { |arg, block|
|
65
|
+
assert_not_nil block
|
66
|
+
block.call
|
67
|
+
}
|
81
68
|
end
|
69
|
+
|
70
|
+
assert_equal :block_result, mock.f(1) { :block_result }
|
82
71
|
end
|
83
72
|
|
84
73
|
def test_recording_mode_should_validate_args_with_equals
|
85
|
-
|
74
|
+
assert_failure do
|
86
75
|
FlexMock.use("mock") do |mock|
|
87
76
|
mock.should_expect do |r|
|
88
77
|
r.f(1)
|
@@ -93,7 +82,7 @@ class TestRecordMode < Test::Unit::TestCase
|
|
93
82
|
end
|
94
83
|
|
95
84
|
def test_recording_mode_should_allow_arg_contraint_validation
|
96
|
-
|
85
|
+
assert_failure do
|
97
86
|
FlexMock.use("mock") do |mock|
|
98
87
|
mock.should_expect do |r|
|
99
88
|
r.f(1)
|
@@ -104,7 +93,7 @@ class TestRecordMode < Test::Unit::TestCase
|
|
104
93
|
end
|
105
94
|
|
106
95
|
def test_recording_mode_should_handle_multiplicity_contraints
|
107
|
-
|
96
|
+
assert_failure do
|
108
97
|
FlexMock.use("mock") do |mock|
|
109
98
|
mock.should_expect do |r|
|
110
99
|
r.f { :result }.once
|
@@ -116,7 +105,7 @@ class TestRecordMode < Test::Unit::TestCase
|
|
116
105
|
end
|
117
106
|
|
118
107
|
def test_strict_record_mode_requires_exact_argument_matches
|
119
|
-
|
108
|
+
assert_failure do
|
120
109
|
FlexMock.use("mock") do |mock|
|
121
110
|
mock.should_expect do |rec|
|
122
111
|
rec.should_be_strict
|
@@ -128,7 +117,7 @@ class TestRecordMode < Test::Unit::TestCase
|
|
128
117
|
end
|
129
118
|
|
130
119
|
def test_strict_record_mode_requires_exact_ordering
|
131
|
-
|
120
|
+
assert_failure do
|
132
121
|
FlexMock.use("mock") do |mock|
|
133
122
|
mock.should_expect do |rec|
|
134
123
|
rec.should_be_strict
|
@@ -142,7 +131,7 @@ class TestRecordMode < Test::Unit::TestCase
|
|
142
131
|
end
|
143
132
|
|
144
133
|
def test_strict_record_mode_requires_once
|
145
|
-
|
134
|
+
assert_failure do
|
146
135
|
FlexMock.use("mock") do |mock|
|
147
136
|
mock.should_expect do |rec|
|
148
137
|
rec.should_be_strict
|
data/test/test_should_receive.rb
CHANGED
@@ -24,6 +24,8 @@ module Kernel
|
|
24
24
|
end
|
25
25
|
|
26
26
|
class TestFlexMockShoulds < Test::Unit::TestCase
|
27
|
+
include FlexMock::TestCase
|
28
|
+
include FlexMock::FailureAssertion
|
27
29
|
|
28
30
|
# Expected error messages on failures
|
29
31
|
COUNT_ERROR_MESSAGE = /\bcalled\s+incorrect\s+number\s+of\s+times\b/
|
@@ -31,6 +33,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
|
|
31
33
|
AT_LEAST_ERROR_MESSAGE = /\bshould\s+be\s+called\s+at\s+least\b/
|
32
34
|
AT_MOST_ERROR_MESSAGE = /\bshould\s+be\s+called\s+at\s+most\b/
|
33
35
|
OUT_OF_ORDER_ERROR_MESSAGE = /\bcalled\s+out\s+of\s+order\b/
|
36
|
+
NON_CONTAINER_MESSAGE = /\bis\s+not\s+in\s+a\s+container\b/
|
34
37
|
|
35
38
|
def test_defaults
|
36
39
|
FlexMock.use do |m|
|
@@ -60,6 +63,17 @@ class TestFlexMockShoulds < Test::Unit::TestCase
|
|
60
63
|
end
|
61
64
|
end
|
62
65
|
|
66
|
+
def test_multiple_returns
|
67
|
+
FlexMock.use do |m|
|
68
|
+
m.should_receive(:hi).and_return(1).and_return(2,3)
|
69
|
+
assert_equal 1, m.hi
|
70
|
+
assert_equal 2, m.hi
|
71
|
+
assert_equal 3, m.hi
|
72
|
+
assert_equal 3, m.hi
|
73
|
+
assert_equal 3, m.hi
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
63
77
|
def test_returns_with_block
|
64
78
|
FlexMock.use do |m|
|
65
79
|
result = nil
|
@@ -69,6 +83,16 @@ class TestFlexMockShoulds < Test::Unit::TestCase
|
|
69
83
|
end
|
70
84
|
end
|
71
85
|
|
86
|
+
def test_return_with_and_without_block_interleaved
|
87
|
+
FlexMock.use do |m|
|
88
|
+
m.should_receive(:hi).and_return(:a).and_return { :b }.and_return(:c)
|
89
|
+
assert_equal :a, m.hi
|
90
|
+
assert_equal :b, m.hi
|
91
|
+
assert_equal :c, m.hi
|
92
|
+
assert_equal :c, m.hi
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
72
96
|
def test_and_returns_alias
|
73
97
|
FlexMock.use do |m|
|
74
98
|
m.should_receive(:hi).and_return(4)
|
@@ -76,6 +100,83 @@ class TestFlexMockShoulds < Test::Unit::TestCase
|
|
76
100
|
end
|
77
101
|
end
|
78
102
|
|
103
|
+
def test_and_yield_will_continue_to_yield_the_same_value
|
104
|
+
FlexMock.use do |m|
|
105
|
+
m.should_receive(:hi).and_yield(:yield_value)
|
106
|
+
assert_equal :yield_value, m.hi { |v| v }
|
107
|
+
assert_equal :yield_value, m.hi { |v| v }
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_and_yield_with_multiple_values_yields_the_values
|
112
|
+
FlexMock.use do |m|
|
113
|
+
m.should_receive(:hi).and_yield(:one, :two).once
|
114
|
+
assert_equal [:one, :two], m.hi { |a, b| [a, b] }
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_multiple_yields_are_done_sequentially
|
119
|
+
FlexMock.use do |m|
|
120
|
+
m.should_receive(:msg).and_yield(:one).and_yield(:two)
|
121
|
+
assert_equal :one, m.msg { |a| a }
|
122
|
+
assert_equal :two, m.msg { |a| a }
|
123
|
+
assert_equal :two, m.msg { |a| a }
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_failure_if_no_block_given
|
128
|
+
FlexMock.use do |m|
|
129
|
+
m.should_receive(:hi).and_yield(:one, :two).once
|
130
|
+
assert_raise(FlexMock::MockError) do m.hi end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_failure_different_return_value_than_yield_return
|
135
|
+
FlexMock.use do |m|
|
136
|
+
m.should_receive(:hi).and_yield(:yld).once.and_return(:ret)
|
137
|
+
yielded_value = nil
|
138
|
+
assert_equal :ret, m.hi { |v| yielded_value = v }
|
139
|
+
assert_equal :yld, yielded_value
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_multiple_yields
|
144
|
+
FlexMock.use do |m|
|
145
|
+
m.should_receive(:hi).and_yield(:one, :two).and_yield(1, 2)
|
146
|
+
assert_equal [:one, :two], m.hi { |a, b| [a, b] }
|
147
|
+
assert_equal [1, 2], m.hi { |a, b| [a, b] }
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_multiple_yields_will_yield_the_last_value_set
|
152
|
+
FlexMock.use do |m|
|
153
|
+
m.should_receive(:hi).and_yield(:a).and_yield(:b)
|
154
|
+
assert_equal [:a], m.hi { |a, b| [a] }
|
155
|
+
assert_equal [:b], m.hi { |a, b| [a] }
|
156
|
+
assert_equal [:b], m.hi { |a, b| [a] }
|
157
|
+
assert_equal [:b], m.hi { |a, b| [a] }
|
158
|
+
assert_equal [:b], m.hi { |a, b| [a] }
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_yielding_then_not_yielding_and_then_yielding_again
|
163
|
+
FlexMock.use do |m|
|
164
|
+
m.should_receive(:hi).and_yield(:a).once
|
165
|
+
m.should_receive(:hi).and_return(:b).once
|
166
|
+
m.should_receive(:hi).and_yield(:c).once
|
167
|
+
assert_equal :a, m.hi { |v| v }
|
168
|
+
assert_equal :b, m.hi
|
169
|
+
assert_equal :c, m.hi { |v| v }
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
def test_yields_syntax
|
174
|
+
FlexMock.use do |m|
|
175
|
+
m.should_receive(:hi).yields(:one)
|
176
|
+
assert_equal :one, m.hi { |a| a }
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
79
180
|
class MyError < RuntimeError
|
80
181
|
end
|
81
182
|
|
@@ -118,6 +219,63 @@ class TestFlexMockShoulds < Test::Unit::TestCase
|
|
118
219
|
end
|
119
220
|
end
|
120
221
|
|
222
|
+
def test_multiple_and_raise_clauses_will_be_done_sequentially
|
223
|
+
FlexMock.use do |m|
|
224
|
+
m.should_receive(:failure).
|
225
|
+
and_raise(RuntimeError, "ONE").
|
226
|
+
and_raise(RuntimeError, "TWO")
|
227
|
+
ex = assert_raise RuntimeError do m.failure end
|
228
|
+
assert_equal "ONE", ex.message
|
229
|
+
ex = assert_raise RuntimeError do m.failure end
|
230
|
+
assert_equal "TWO", ex.message
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
def test_and_throw_will_throw_a_symbol
|
235
|
+
FlexMock.use do |m|
|
236
|
+
m.should_receive(:msg).and_throw(:sym)
|
237
|
+
value = catch(:sym) do
|
238
|
+
m.msg
|
239
|
+
fail "Should not reach this line"
|
240
|
+
end
|
241
|
+
assert_nil value
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
def test_and_throw_with_expression_will_throw
|
246
|
+
FlexMock.use do |m|
|
247
|
+
m.should_receive(:msg).and_throw(:sym, :return_value)
|
248
|
+
value = catch(:sym) do
|
249
|
+
m.msg
|
250
|
+
fail "Should not reach this line"
|
251
|
+
end
|
252
|
+
assert_equal :return_value, value
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
def test_throws_is_an_alias_for_and_throw
|
257
|
+
FlexMock.use do |m|
|
258
|
+
m.should_receive(:msg).throws(:sym, :return_value)
|
259
|
+
value = catch(:sym) do
|
260
|
+
m.msg
|
261
|
+
fail "Should not reach this line"
|
262
|
+
end
|
263
|
+
assert_equal :return_value, value
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
def test_multiple_throws_will_be_done_sequentially
|
268
|
+
FlexMock.use do |m|
|
269
|
+
m.should_receive(:toss).
|
270
|
+
and_throw(:sym, "ONE").
|
271
|
+
and_throw(:sym, "TWO")
|
272
|
+
value = catch(:sym) do m.toss end
|
273
|
+
assert_equal "ONE", value
|
274
|
+
value = catch(:sym) do m.toss end
|
275
|
+
assert_equal "TWO", value
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
121
279
|
def test_multiple_expectations
|
122
280
|
FlexMock.use do |m|
|
123
281
|
m.should_receive(:hi).with(1).returns(10)
|
@@ -135,7 +293,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
|
|
135
293
|
end
|
136
294
|
end
|
137
295
|
|
138
|
-
def
|
296
|
+
def test_with_no_args_but_with_args
|
139
297
|
ex = assert_failure(NO_MATCH_ERROR_MESSAGE) do
|
140
298
|
FlexMock.use do |m|
|
141
299
|
m.should_receive(:hi).with_no_args
|
@@ -480,7 +638,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
|
|
480
638
|
assert_match(/hi\(2\)/, ex.message)
|
481
639
|
end
|
482
640
|
|
483
|
-
def
|
641
|
+
def test_ordered_calls_in_order_will_pass
|
484
642
|
FlexMock.use 'm' do |m|
|
485
643
|
m.should_receive(:hi).ordered
|
486
644
|
m.should_receive(:lo).ordered
|
@@ -490,7 +648,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
|
|
490
648
|
end
|
491
649
|
end
|
492
650
|
|
493
|
-
def
|
651
|
+
def test_ordered_calls_out_of_order_will_fail
|
494
652
|
ex = assert_failure(OUT_OF_ORDER_ERROR_MESSAGE) do
|
495
653
|
FlexMock.use 'm' do |m|
|
496
654
|
m.should_receive(:hi).ordered
|
@@ -502,7 +660,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
|
|
502
660
|
end
|
503
661
|
end
|
504
662
|
|
505
|
-
def
|
663
|
+
def test_order_calls_with_different_arg_lists_and_in_order_will_pass
|
506
664
|
FlexMock.use 'm' do |m|
|
507
665
|
m.should_receive(:hi).with("one").ordered
|
508
666
|
m.should_receive(:hi).with("two").ordered
|
@@ -512,7 +670,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
|
|
512
670
|
end
|
513
671
|
end
|
514
672
|
|
515
|
-
def
|
673
|
+
def test_order_calls_with_different_arg_lists_and_out_of_order_will_fail
|
516
674
|
ex = assert_failure(OUT_OF_ORDER_ERROR_MESSAGE) do
|
517
675
|
FlexMock.use 'm' do |m|
|
518
676
|
m.should_receive(:hi).with("one").ordered
|
@@ -538,7 +696,7 @@ class TestFlexMockShoulds < Test::Unit::TestCase
|
|
538
696
|
end
|
539
697
|
end
|
540
698
|
|
541
|
-
def
|
699
|
+
def test_ordered_with_multiple_calls_will_pass
|
542
700
|
FlexMock.use 'm' do |m|
|
543
701
|
m.should_receive(:hi).ordered
|
544
702
|
m.should_receive(:lo).ordered
|
@@ -628,14 +786,43 @@ class TestFlexMockShoulds < Test::Unit::TestCase
|
|
628
786
|
m.foo
|
629
787
|
end
|
630
788
|
end
|
789
|
+
|
790
|
+
def test_ordering_between_mocks_is_not_normally_defined
|
791
|
+
FlexMock.use("x", "y") do |x, y|
|
792
|
+
x.should_receive(:one).ordered
|
793
|
+
y.should_receive(:two).ordered
|
794
|
+
|
795
|
+
assert_nothing_raised do
|
796
|
+
y.two
|
797
|
+
x.one
|
798
|
+
end
|
799
|
+
end
|
800
|
+
end
|
631
801
|
|
802
|
+
def test_ordering_between_mocks_is_honored_for_global_ordering
|
803
|
+
ex = assert_failure(OUT_OF_ORDER_ERROR_MESSAGE) do
|
804
|
+
FlexMock.use("x", "y") do |x, y|
|
805
|
+
x.should_receive(:one).globally.ordered
|
806
|
+
y.should_receive(:two).globally.ordered
|
807
|
+
|
808
|
+
y.two
|
809
|
+
x.one
|
810
|
+
end
|
811
|
+
end
|
812
|
+
end
|
813
|
+
|
632
814
|
def test_expectation_formating
|
633
|
-
|
815
|
+
mock = flexmock("m")
|
816
|
+
exp = mock.should_receive(:f).with(1,"two", /^3$/).
|
817
|
+
and_return(0).at_least.once
|
818
|
+
|
819
|
+
mock.f(1, "two", 3)
|
634
820
|
assert_equal 'f(1, "two", /^3$/)', exp.to_s
|
635
821
|
end
|
636
822
|
|
637
823
|
def test_multi_expectation_formatting
|
638
|
-
|
824
|
+
mock = flexmock("mock")
|
825
|
+
exp = mock.should_receive(:f, :g).with(1)
|
639
826
|
assert_equal "[f(1), g(1)]", exp.to_s
|
640
827
|
end
|
641
828
|
|
@@ -659,40 +846,25 @@ class TestFlexMockShoulds < Test::Unit::TestCase
|
|
659
846
|
end
|
660
847
|
|
661
848
|
def test_global_methods_can_be_mocked
|
662
|
-
m =
|
849
|
+
m = flexmock("m")
|
663
850
|
m.should_receive(:mock_top_level_function).and_return(:mock)
|
664
851
|
assert_equal :mock, m.mock_top_level_function
|
665
852
|
end
|
666
853
|
|
667
854
|
def test_kernel_methods_can_be_mocked
|
668
|
-
m =
|
855
|
+
m = flexmock("m")
|
669
856
|
m.should_receive(:mock_kernel_function).and_return(:mock)
|
670
857
|
assert_equal :mock, m.mock_kernel_function
|
671
858
|
end
|
672
859
|
|
673
860
|
def test_undefing_kernel_methods_dont_effect_other_mocks
|
674
|
-
m =
|
675
|
-
m2 =
|
861
|
+
m = flexmock("m")
|
862
|
+
m2 = flexmock("m2")
|
676
863
|
m.should_receive(:mock_kernel_function).and_return(:mock)
|
677
864
|
assert_equal :mock, m.mock_kernel_function
|
678
865
|
assert_equal :mkf, m2.mock_kernel_function
|
679
866
|
end
|
680
867
|
|
681
|
-
# Assertion helper used to assert validation failure. If a
|
682
|
-
# message is given, then the error message should match the
|
683
|
-
# expected error message.
|
684
|
-
def assert_failure(message=nil)
|
685
|
-
ex = assert_raises(Test::Unit::AssertionFailedError) { yield }
|
686
|
-
if message
|
687
|
-
case message
|
688
|
-
when Regexp
|
689
|
-
assert_match message, ex.message
|
690
|
-
when String
|
691
|
-
assert ex.message.index(message), "Error message '#{ex.message}' should contain '#{message}'"
|
692
|
-
end
|
693
|
-
end
|
694
|
-
ex
|
695
|
-
end
|
696
868
|
end
|
697
869
|
|
698
870
|
class TestFlexMockShouldsWithInclude < Test::Unit::TestCase
|