hardmock 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,192 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+ require 'fileutils'
3
+
4
+ class AutoVerifyTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @expect_unmet_expectations = true
8
+ end
9
+
10
+ def teardown
11
+ remove_temp_test_file
12
+ end
13
+
14
+ #
15
+ # HELPERS
16
+ #
17
+
18
+ def temp_test_file
19
+ File.expand_path(File.dirname(__FILE__) + "/tear_down_verification_test.rb")
20
+ end
21
+
22
+ def run_test(tbody)
23
+ File.open(temp_test_file,"w") { |f| f.print(tbody) }
24
+ @test_output = `ruby #{temp_test_file} 2>&1`
25
+ # puts "------------------------"
26
+ # puts @test_output
27
+ # puts "------------------------"
28
+ end
29
+
30
+ def remove_temp_test_file
31
+ FileUtils::rm_f temp_test_file
32
+ end
33
+
34
+ def assert_results(h)
35
+ assert_match(/#{h[:tests]} tests, [0-9]+ assertions, #{h[:failures]} failures, #{h[:errors]} errors/,
36
+ @test_output)
37
+ end
38
+
39
+ def assert_output_contains(*patterns)
40
+ patterns.each do |pattern|
41
+ assert_match(pattern,@test_output)
42
+ end
43
+ end
44
+
45
+ def assert_output_doesnt_contain(*patterns)
46
+ patterns.each do |pattern|
47
+ assert @test_output !~ pattern, "Output shouldn't match #{pattern.inspect} but it does."
48
+ end
49
+ end
50
+
51
+ def write_and_execute_test
52
+ @test_code ||=<<-EOM
53
+ def test_setup_doomed_expectation
54
+ create_mock :automobile
55
+ @automobile.expects.start
56
+ end
57
+ EOM
58
+ @full_code ||=<<-EOTEST
59
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
60
+ require 'hardmock'
61
+ class DummyTest < Test::Unit::TestCase
62
+ #{@teardown_code}
63
+ #{@test_code}
64
+ end
65
+ EOTEST
66
+ run_test @full_code
67
+
68
+ if @expect_unmet_expectations
69
+ assert_output_contains(/unmet expectations/i, /automobile/, /start/)
70
+ else
71
+ assert_output_doesnt_contain(/unmet expectations/i, /automobile/, /start/)
72
+ end
73
+
74
+ @expect_tests ||= 1
75
+ @expect_failures ||= 0
76
+ @expect_errors ||= 1
77
+ assert_results :tests => @expect_tests, :failures => @expect_failures, :errors => @expect_errors
78
+ end
79
+
80
+ #
81
+ # TESTS
82
+ #
83
+
84
+ def test_should_auto_verify_mocks_in_teardown
85
+ write_and_execute_test
86
+ end
87
+
88
+ def test_should_auto_verify_mocks_even_if_user_has_own_teardown
89
+ @teardown_code =<<-EOM
90
+ def teardown
91
+ # just in the way
92
+ end
93
+ EOM
94
+ write_and_execute_test
95
+ end
96
+
97
+ def test_should_auto_verify_mocks_but_not_obscure_natural_failures
98
+ @test_code =<<-EOM
99
+ def test_setup_doomed_expectation
100
+ create_mock :automobile
101
+ @automobile.expects.start
102
+ flunk "natural failure"
103
+ end
104
+ EOM
105
+ @expect_failures = 1
106
+ write_and_execute_test
107
+ end
108
+
109
+ def test_should_not_prevent_user_teardown_even_if_verification_fails
110
+ @teardown_code =<<-EOM
111
+ def teardown
112
+ puts "User teardown"
113
+ end
114
+ EOM
115
+ write_and_execute_test
116
+ assert_output_contains(/User teardown/)
117
+ end
118
+
119
+ def test_should_not_raise_error_if_verification_goes_according_to_plan
120
+ @test_code =<<-EOM
121
+ def test_ok
122
+ create_mock :automobile
123
+ @automobile.expects.start
124
+ @automobile.start
125
+ end
126
+ EOM
127
+ @teardown_code =<<-EOM
128
+ def teardown
129
+ puts "User teardown"
130
+ end
131
+ EOM
132
+ @expect_unmet_expectations = false
133
+ @expect_failures = 0
134
+ @expect_errors = 0
135
+ write_and_execute_test
136
+ assert_output_contains(/User teardown/)
137
+ end
138
+
139
+ def test_should_not_do_verification_if_user_teardown_explodes
140
+ @teardown_code =<<-EOM
141
+ def teardown
142
+ raise "self destruct"
143
+ end
144
+ EOM
145
+ @expect_unmet_expectations = false
146
+ write_and_execute_test
147
+ assert_output_contains(/self destruct/)
148
+ end
149
+
150
+ def test_should_not_obscure_inherited_teardown
151
+ @full_code ||=<<-EOTEST
152
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
153
+ require 'hardmock'
154
+ class Test::Unit::TestCase
155
+ def teardown
156
+ puts "Test helper teardown"
157
+ end
158
+ end
159
+ class DummyTest < Test::Unit::TestCase
160
+ def test_prepare_to_die
161
+ create_mock :automobile
162
+ @automobile.expects.start
163
+ end
164
+ end
165
+ EOTEST
166
+ write_and_execute_test
167
+ assert_output_contains(/Test helper teardown/)
168
+ end
169
+
170
+ def test_should_simultaneously_support_inherited_and_user_and_hardmock_teardown
171
+ @full_code ||=<<-EOTEST
172
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
173
+ class Test::Unit::TestCase
174
+ def teardown
175
+ puts "Test helper teardown"
176
+ end
177
+ end
178
+ require 'hardmock' # IMPORTANT TO DO THIS HERE, between the old and new teardown defs
179
+ class DummyTest < Test::Unit::TestCase
180
+ def teardown
181
+ puts "User teardown"
182
+ end
183
+ def test_prepare_to_die
184
+ create_mock :automobile
185
+ @automobile.expects.start
186
+ end
187
+ end
188
+ EOTEST
189
+ write_and_execute_test
190
+ assert_output_contains(/Test helper teardown/, /User teardown/)
191
+ end
192
+ end
@@ -0,0 +1,396 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
2
+ require 'hardmock'
3
+
4
+ class DirectMockUsageTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @bird = Mock.new('bird')
8
+ end
9
+
10
+ def teardown
11
+ end
12
+
13
+ #
14
+ # TESTS
15
+ #
16
+
17
+ def test_verify_should_raise_verify_error_if_expected_method_not_called
18
+ @bird.expects.flap_flap
19
+
20
+ err = assert_raise VerifyError do
21
+ @bird._verify
22
+ end
23
+ assert_match(/unmet expectations/i, err.message)
24
+ end
25
+
26
+ def test_verify_should_not_raise_when_expected_calls_made_in_order
27
+ @bird.expects.flap_flap
28
+ @bird.expects.bang
29
+ @bird.expects.plop
30
+
31
+ @bird.flap_flap
32
+ @bird.bang
33
+ @bird.plop
34
+
35
+ @bird._verify
36
+ end
37
+
38
+ def test_should_raise_expectation_error_when_unexpected_method_called
39
+ @bird.expects.flap_flap
40
+
41
+ err = assert_raise ExpectationError do
42
+ @bird.shoot
43
+ end
44
+ assert_match(/wrong method/i, err.message)
45
+ end
46
+
47
+ def test_bad_argument_call
48
+ @bird.expects.flap_flap(:swoosh)
49
+
50
+ err = assert_raise ExpectationError do
51
+ @bird.flap_flap(:rip)
52
+ end
53
+ assert_match(/wrong arguments/i, err.message)
54
+ end
55
+
56
+ def test_verify_should_raise_verify_error_when_not_all_expected_methods_called
57
+ @bird.expects.flap_flap
58
+ @bird.expects.bang
59
+ @bird.expects.plop
60
+
61
+ @bird.flap_flap
62
+
63
+ err = assert_raise VerifyError do
64
+ @bird._verify
65
+ end
66
+ assert_match(/unmet expectations/i, err.message)
67
+ end
68
+
69
+ def test_should_raise_expectation_error_when_calls_made_out_of_order
70
+ @bird.expects.flap_flap
71
+ @bird.expects.bang
72
+ @bird.expects.plop
73
+
74
+ @bird.flap_flap
75
+ err = assert_raise ExpectationError do
76
+ @bird.plop
77
+ end
78
+ assert_match(/wrong method/i, err.message)
79
+ end
80
+
81
+ def test_should_return_given_value_when_specified
82
+ @bird.expects.plop.returns(':P')
83
+ assert_equal ':P', @bird.plop
84
+ @bird._verify
85
+
86
+ @bird.expects.plop.returns(':x')
87
+ assert_equal ':x', @bird.plop
88
+ @bird._verify
89
+ end
90
+
91
+ def test_should_return_nil_value_when_none_specified
92
+ @bird.expects.plop
93
+ assert_nil @bird.plop
94
+ @bird._verify
95
+ end
96
+
97
+ def test_raise_should_raise_given_exception_when_specified
98
+ err = RuntimeError.new('shaq')
99
+ @bird.expects.plop.raises(err)
100
+ actual_err = assert_raise RuntimeError do
101
+ @bird.plop
102
+ end
103
+ assert_same err, actual_err, 'should be the same error'
104
+ @bird._verify
105
+ end
106
+
107
+ def test_raise_should_raise_given_string_wrapped_in_runtime_error
108
+ @bird.expects.plop.raises('shaq')
109
+ err = assert_raise RuntimeError do
110
+ @bird.plop
111
+ end
112
+ assert_match(/shaq/i, err.message)
113
+ @bird._verify
114
+ end
115
+
116
+ def test_raise_should_raise_a_canned_runtime_error_if_nothing_given
117
+ @bird.expects.plop.raises
118
+ err = assert_raise RuntimeError do
119
+ @bird.plop
120
+ end
121
+ assert_match(/error/i, err.message)
122
+ @bird._verify
123
+ end
124
+
125
+ def test_should_be_happy_with_correct_expected_arguments
126
+ thing = Object.new
127
+ @bird.expects.plop(:big,'one',thing)
128
+ @bird.plop(:big,'one',thing)
129
+ @bird._verify
130
+ end
131
+
132
+ def test_should_raise_expectation_error_when_wrong_number_of_arguemnts_specified
133
+ thing = Object.new
134
+ @bird.expects.plop(:big,'one',thing)
135
+ err = assert_raise ExpectationError do
136
+ # more
137
+ @bird.plop(:big,'one',thing,:other)
138
+ end
139
+ assert_match(/wrong arguments/i, err.message)
140
+ @bird._verify
141
+
142
+ @bird.expects.plop(:big,'one',thing)
143
+ err = assert_raise ExpectationError do
144
+ # less
145
+ @bird.plop(:big,'one')
146
+ end
147
+ assert_match(/wrong arguments/i, err.message)
148
+ @bird._verify
149
+
150
+ @bird.expects.plop
151
+ err = assert_raise ExpectationError do
152
+ # less
153
+ @bird.plop(:big)
154
+ end
155
+ assert_match(/wrong arguments/i, err.message)
156
+ @bird._verify
157
+ end
158
+
159
+ def test_should_raise_expectation_error_when_arguemnts_dont_match
160
+ thing = Object.new
161
+ @bird.expects.plop(:big,'one',thing)
162
+ err = assert_raise ExpectationError do
163
+ @bird.plop(:big,'two',thing,:other)
164
+ end
165
+ assert_match(/wrong arguments/i, err.message)
166
+ @bird._verify
167
+ end
168
+
169
+ def test_should_yield_to_block_given
170
+ mitt = nil
171
+ @bird.expects.plop { mitt = :ball }
172
+ assert_nil mitt
173
+ @bird.plop
174
+ assert_equal :ball, mitt, 'didnt catch the ball'
175
+ @bird._verify
176
+
177
+ @bird.expects.plop { raise 'ball' }
178
+ err = assert_raise RuntimeError do
179
+ @bird.plop
180
+ end
181
+ assert_match(/ball/i, err.message)
182
+ @bird._verify
183
+ end
184
+
185
+ def test_shouldnt_care_about_arguments_if_block_given
186
+ ball = nil
187
+ mitt = nil
188
+ @bird.expects.plop {|arg1,arg2|
189
+ ball = arg1
190
+ mitt = arg2
191
+ }
192
+ assert_nil ball
193
+ assert_nil mitt
194
+ @bird.plop(:ball,:mitt)
195
+ assert_equal :ball, ball
196
+ assert_equal :mitt, mitt
197
+ @bird._verify
198
+ end
199
+
200
+ def test_should_check_arguments_if_specified_when_block_given
201
+ ball = nil
202
+ mitt = nil
203
+ @bird.expects.plop(:ball,:mitt) {|arg1,arg2|
204
+ ball = arg1
205
+ mitt = arg2
206
+ }
207
+ assert_nil ball
208
+ assert_nil mitt
209
+ @bird.plop(:ball,:mitt)
210
+ assert_equal :ball, ball
211
+ assert_equal :mitt, mitt
212
+ @bird._verify
213
+
214
+ ball = nil
215
+ mitt = nil
216
+ @bird.expects.plop(:bad,:stupid) {|arg1,arg2|
217
+ ball = arg1
218
+ mitt = arg2
219
+ }
220
+ assert_nil ball
221
+ assert_nil mitt
222
+ err = assert_raise ExpectationError do
223
+ @bird.plop(:ball,:mitt)
224
+ end
225
+ assert_match(/wrong arguments/i, err.message)
226
+ assert_nil ball
227
+ assert_nil mitt
228
+ @bird._verify
229
+
230
+ ball = nil
231
+ mitt = nil
232
+ @bird.expects.plop(:ball,:mitt) {|arg1,arg2|
233
+ ball = arg1
234
+ mitt = arg2
235
+ }
236
+ assert_nil ball
237
+ assert_nil mitt
238
+ err = assert_raise ExpectationError do
239
+ @bird.plop(:ball)
240
+ end
241
+ assert_match(/wrong arguments/i, err.message)
242
+ assert_nil ball
243
+ assert_nil mitt
244
+ @bird._verify
245
+ end
246
+
247
+ def test_runtime_blocks_get_passed_to_expectation_blocks
248
+ runtime_block_called = false
249
+ got_arg = nil
250
+
251
+ # Eg, bird expects someone to subscribe to :tweet using the 'when' method
252
+ @bird.expects.when(:tweet) { |arg1, block|
253
+ got_arg = arg1
254
+ block.call
255
+ }
256
+
257
+ @bird.when(:tweet) do
258
+ runtime_block_called = true
259
+ end
260
+
261
+ assert_equal :tweet, got_arg, "Wrong arg"
262
+ assert runtime_block_called, "The runtime block should have been invoked by the user block"
263
+
264
+ @bird.expects.when(:warnk) { |e,blk| }
265
+
266
+ err = assert_raise ExpectationError do
267
+ @bird.when(:honk) { }
268
+ end
269
+ assert_match(/wrong arguments/i, err.message)
270
+
271
+ @bird._verify
272
+ end
273
+
274
+ def test_runtime_blocks_get_passed_to_expectation_blocks__no_arguments
275
+ runtime_block_called = false
276
+ @bird.expects.subscribe { |block| block.call }
277
+ @bird.subscribe do
278
+ runtime_block_called = true
279
+ end
280
+ assert runtime_block_called, "The runtime block should have been invoked by the user block"
281
+ end
282
+
283
+ def test_expect_runtime_block_but_none_sent
284
+ invoked = false
285
+ @bird.expects.kablam(:scatter) { |shot,block|
286
+ assert_equal :scatter, shot, "Wrong shot"
287
+ assert_nil block, "The expectation block should get a nil block when user neglects to pass one"
288
+ invoked = true
289
+ }
290
+ @bird.kablam :scatter
291
+ assert invoked, "Expectation block not invoked"
292
+
293
+ @bird._verify
294
+ end
295
+
296
+ def test_can_set_return_after_blocks
297
+ got = nil
298
+ @bird.expects.kablam(:scatter) { |shot|
299
+ got = shot
300
+ }.returns(:death)
301
+
302
+ val = @bird.kablam :scatter
303
+ assert_equal :death, val, "Wrong return value"
304
+ assert_equal :scatter, got, "Wrong argument"
305
+ @bird._verify
306
+ end
307
+
308
+ def test_can_set_raises_after_blocks
309
+ got = nil
310
+ @bird.expects.kablam(:scatter) do |shot|
311
+ got = shot
312
+ end.raises "hell"
313
+
314
+ err = assert_raise RuntimeError do
315
+ @bird.kablam :scatter
316
+ end
317
+ assert_match(/hell/i, err.message)
318
+
319
+ @bird._verify
320
+ end
321
+
322
+ def test_expectation_block_value_is_captured
323
+ expectation = @bird.expects.kablam(:slug) { |shot|
324
+ "The shot was #{shot}"
325
+ }
326
+
327
+ assert_not_nil expectation, "Expectation nil"
328
+ assert_nil expectation.block_value, "Block value should start out nil"
329
+
330
+ ret_val = @bird.kablam :slug
331
+
332
+ assert_equal "The shot was slug", expectation.block_value
333
+ assert_equal "The shot was slug", ret_val, "Block value should also be used for return"
334
+
335
+ @bird._verify
336
+ end
337
+
338
+
339
+ def test_expectation_block_value_is_used_for_return_value
340
+ @bird.expects.kablam(:scatter) { |shot|
341
+ "The shot was #{shot}"
342
+ }
343
+ val = @bird.kablam :scatter
344
+ assert_equal "The shot was scatter", val, "Wrong return value"
345
+ @bird._verify
346
+ end
347
+
348
+ def test_expectation_is_still_returned_when_using_returns
349
+ expectation = @bird.expects.kablam(:slug) { |shot|
350
+ "The shot was #{shot}"
351
+ }.returns :hosed
352
+
353
+ assert_not_nil expectation, "Expectation nil"
354
+ assert_nil expectation.block_value, "Block value should start out nil"
355
+
356
+ ret_val = @bird.kablam :slug
357
+
358
+ assert_equal "The shot was slug", expectation.block_value
359
+ assert_equal :hosed, ret_val, "Block value should also be used for return"
360
+
361
+ @bird._verify
362
+ end
363
+
364
+ def test_expectation_is_still_returned_when_using_raises
365
+ expectation = @bird.expects.kablam(:slug) { |shot|
366
+ "The shot was #{shot}"
367
+ }.raises "aiee!"
368
+
369
+ assert_not_nil expectation, "Expectation nil"
370
+ assert_nil expectation.block_value, "Block value should start out nil"
371
+
372
+ err = assert_raise RuntimeError do
373
+ @bird.kablam :slug
374
+ end
375
+ assert_match(/aiee!/i, err.message)
376
+ assert_equal "The shot was slug", expectation.block_value
377
+ @bird._verify
378
+ end
379
+
380
+
381
+ def test_expect_assignment
382
+ @bird.expects.size = "large"
383
+ @bird.size = "large"
384
+ @bird._verify
385
+ end
386
+
387
+ def test_expect_assignment_with_raise
388
+ @bird.expects('size=','large').raises "boom"
389
+
390
+ err = assert_raise RuntimeError do
391
+ @bird.size = "large"
392
+ end
393
+ assert_match(/boom/i, err.message)
394
+ end
395
+
396
+ end