File: expectation_test.rb

package info (click to toggle)
ruby-mocha 0.11.3-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,300 kB
  • sloc: ruby: 9,935; makefile: 2
file content (480 lines) | stat: -rw-r--r-- 16,522 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
require File.expand_path('../../test_helper', __FILE__)
require 'method_definer'
require 'mocha/expectation'
require 'mocha/sequence'
require 'execution_point'
require 'simple_counter'

class ExpectationTest < Test::Unit::TestCase

  include Mocha

  def new_expectation
    Expectation.new(nil, :expected_method)
  end

  def test_should_match_calls_to_same_method_with_any_parameters
    assert new_expectation.match?(:expected_method, 1, 2, 3)
  end

  def test_should_match_calls_to_same_method_with_exactly_zero_parameters
    expectation = new_expectation.with()
    assert expectation.match?(:expected_method)
  end

  def test_should_not_match_calls_to_same_method_with_more_than_zero_parameters
    expectation = new_expectation.with()
    assert !expectation.match?(:expected_method, 1, 2, 3)
  end

  def test_should_match_calls_to_same_method_with_expected_parameter_values
    expectation = new_expectation.with(1, 2, 3)
    assert expectation.match?(:expected_method, 1, 2, 3)
  end

  def test_should_match_calls_to_same_method_with_parameters_constrained_as_expected
    expectation = new_expectation.with() {|x, y, z| x + y == z}
    assert expectation.match?(:expected_method, 1, 2, 3)
  end

  def test_should_not_match_calls_to_different_method_with_parameters_constrained_as_expected
    expectation = new_expectation.with() {|x, y, z| x + y == z}
    assert !expectation.match?(:different_method, 1, 2, 3)
  end

  def test_should_not_match_calls_to_different_methods_with_no_parameters
    assert !new_expectation.match?(:unexpected_method)
  end

  def test_should_not_match_calls_to_same_method_with_too_few_parameters
    expectation = new_expectation.with(1, 2, 3)
    assert !expectation.match?(:unexpected_method, 1, 2)
  end

  def test_should_not_match_calls_to_same_method_with_too_many_parameters
    expectation = new_expectation.with(1, 2)
    assert !expectation.match?(:unexpected_method, 1, 2, 3)
  end

  def test_should_not_match_calls_to_same_method_with_unexpected_parameter_values
    expectation = new_expectation.with(1, 2, 3)
    assert !expectation.match?(:unexpected_method, 1, 0, 3)
  end

  def test_should_not_match_calls_to_same_method_with_parameters_not_constrained_as_expected
    expectation = new_expectation.with() {|x, y, z| x + y == z}
    assert !expectation.match?(:expected_method, 1, 0, 3)
  end

  def test_should_allow_invocations_until_expected_invocation_count_is_one_and_actual_invocation_count_would_be_two
    expectation = new_expectation.times(1)
    assert expectation.invocations_allowed?
    expectation.invoke
    assert !expectation.invocations_allowed?
  end

  def test_should_allow_invocations_until_expected_invocation_count_is_two_and_actual_invocation_count_would_be_three
    expectation = new_expectation.times(2)
    assert expectation.invocations_allowed?
    expectation.invoke
    assert expectation.invocations_allowed?
    expectation.invoke
    assert !expectation.invocations_allowed?
  end

  def test_should_allow_invocations_until_expected_invocation_count_is_a_range_from_two_to_three_and_actual_invocation_count_would_be_four
    expectation = new_expectation.times(2..3)
    assert expectation.invocations_allowed?
    expectation.invoke
    assert expectation.invocations_allowed?
    expectation.invoke
    assert expectation.invocations_allowed?
    expectation.invoke
    assert !expectation.invocations_allowed?
  end

  def test_should_store_provided_backtrace
    backtrace = Object.new
    expectation = Expectation.new(nil, :expected_method, backtrace)
    assert_equal backtrace, expectation.backtrace
  end

  def test_should_default_backtrace_to_caller
    execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
    assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
  end

  def test_should_not_yield
    yielded = false
    new_expectation.invoke() { yielded = true }
    assert_equal false, yielded
  end

  def test_should_yield_no_parameters
    expectation = new_expectation().yields()
    yielded_parameters = nil
    expectation.invoke() { |*parameters| yielded_parameters = parameters }
    assert_equal Array.new, yielded_parameters
  end

  def test_should_yield_with_specified_parameters
    expectation = new_expectation().yields(1, 2, 3)
    yielded_parameters = nil
    expectation.invoke() { |*parameters| yielded_parameters = parameters }
    assert_equal [1, 2, 3], yielded_parameters
  end

  def test_should_yield_different_parameters_on_consecutive_invocations
    expectation = new_expectation().yields(1, 2, 3).yields(4, 5)
    yielded_parameters = []
    expectation.invoke() { |*parameters| yielded_parameters << parameters }
    expectation.invoke() { |*parameters| yielded_parameters << parameters }
    assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
  end

  def test_should_yield_multiple_times_for_single_invocation
    expectation = new_expectation().multiple_yields([1, 2, 3], [4, 5])
    yielded_parameters = []
    expectation.invoke() { |*parameters| yielded_parameters << parameters }
    assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
  end

  def test_should_yield_multiple_times_for_first_invocation_and_once_for_second_invocation
    expectation = new_expectation().multiple_yields([1, 2, 3], [4, 5]).then.yields(6, 7)
    yielded_parameters = []
    expectation.invoke() { |*parameters| yielded_parameters << parameters }
    expectation.invoke() { |*parameters| yielded_parameters << parameters }
    assert_equal [[1, 2, 3], [4, 5], [6, 7]], yielded_parameters
  end

  def test_should_return_specified_value
    expectation = new_expectation.returns(99)
    assert_equal 99, expectation.invoke
  end

  def test_should_return_same_specified_value_multiple_times
    expectation = new_expectation.returns(99)
    assert_equal 99, expectation.invoke
    assert_equal 99, expectation.invoke
  end

  def test_should_return_specified_values_on_consecutive_calls
    expectation = new_expectation.returns(99, 100, 101)
    assert_equal 99, expectation.invoke
    assert_equal 100, expectation.invoke
    assert_equal 101, expectation.invoke
  end

  def test_should_return_specified_values_on_consecutive_calls_even_if_values_are_modified
    values = [99, 100, 101]
    expectation = new_expectation.returns(*values)
    values.shift
    assert_equal 99, expectation.invoke
    assert_equal 100, expectation.invoke
    assert_equal 101, expectation.invoke
  end

  def test_should_return_nil_by_default
    assert_nil new_expectation.invoke
  end

  def test_should_return_nil_if_no_value_specified
    expectation = new_expectation.returns()
    assert_nil expectation.invoke
  end

  def test_should_raise_runtime_exception
    expectation = new_expectation.raises
    assert_raise(RuntimeError) { expectation.invoke }
  end

  def test_should_raise_custom_exception
    exception = Class.new(Exception)
    expectation = new_expectation.raises(exception)
    assert_raise(exception) { expectation.invoke }
  end

  def test_should_raise_same_instance_of_custom_exception
    exception_klass = Class.new(StandardError)
    expected_exception = exception_klass.new
    expectation = new_expectation.raises(expected_exception)
    actual_exception = assert_raise(exception_klass) { expectation.invoke }
    assert_same expected_exception, actual_exception
  end

  def test_should_use_the_default_exception_message
    expectation = new_expectation.raises(Exception)
    exception = assert_raise(Exception) { expectation.invoke }
    assert_equal Exception.new.message, exception.message
  end

  def test_should_raise_custom_exception_with_message
    exception_msg = "exception message"
    expectation = new_expectation.raises(Exception, exception_msg)
    exception = assert_raise(Exception) { expectation.invoke }
    assert_equal exception_msg, exception.message
  end

  def test_should_return_values_then_raise_exception
    expectation = new_expectation.returns(1, 2).then.raises()
    assert_equal 1, expectation.invoke
    assert_equal 2, expectation.invoke
    assert_raise(RuntimeError) { expectation.invoke }
  end

  def test_should_raise_exception_then_return_values
    expectation = new_expectation.raises().then.returns(1, 2)
    assert_raise(RuntimeError) { expectation.invoke }
    assert_equal 1, expectation.invoke
    assert_equal 2, expectation.invoke
  end

  def test_should_verify_successfully_if_expected_call_was_made
    expectation = new_expectation
    expectation.invoke
    assert expectation.verified?
  end

  def test_should_not_verify_successfully_if_call_expected_once_but_invoked_twice
    expectation = new_expectation.once
    expectation.invoke
    expectation.invoke
    assert !expectation.verified?
  end

  def test_should_not_verify_successfully_if_call_expected_once_but_not_invoked
    expectation = new_expectation.once
    assert !expectation.verified?
  end

  def test_should_verify_successfully_if_call_expected_once_and_invoked_once
    expectation = new_expectation.once
    expectation.invoke
    assert expectation.verified?
  end

  def test_should_not_verify_successfully_if_call_expected_twice_and_invoked_three_times
    expectation = new_expectation.twice
    expectation.invoke
    expectation.invoke
    expectation.invoke
    assert !expectation.verified?
  end

  def test_should_not_verify_successfully_if_call_expected_twice_but_invoked_once
    expectation = new_expectation.twice
    expectation.invoke
    assert !expectation.verified?
  end

  def test_should_verify_successfully_if_call_expected_twice_and_invoked_twice
    expectation = new_expectation.twice
    expectation.invoke
    expectation.invoke
    assert expectation.verified?
  end

  def test_should_verify_successfully_if_expected_call_was_made_at_least_once
    expectation = new_expectation.at_least_once
    3.times {expectation.invoke}
    assert expectation.verified?
  end

  def test_should_not_verify_successfully_if_expected_call_was_not_made_at_least_once
    expectation = new_expectation.with(1, 2, 3).at_least_once
    assert !expectation.verified?
    assert_match(/expected at least once, not yet invoked/i, expectation.mocha_inspect)
  end

  def test_should_verify_successfully_if_expected_call_was_made_expected_number_of_times
    expectation = new_expectation.times(2)
    2.times {expectation.invoke}
    assert expectation.verified?
  end

  def test_should_not_verify_successfully_if_expected_call_was_made_too_few_times
    expectation = new_expectation.times(2)
    1.times {expectation.invoke}
    assert !expectation.verified?
    assert_match(/expected exactly twice, invoked once/i, expectation.mocha_inspect)
  end

  def test_should_not_verify_successfully_if_expected_call_was_made_too_many_times
    expectation = new_expectation.times(2)
    3.times {expectation.invoke}
    assert !expectation.verified?
  end

  def test_should_increment_assertion_counter_for_expectation_because_it_does_need_verifyng
    expectation = new_expectation
    expectation.invoke
    assertion_counter = SimpleCounter.new
    expectation.verified?(assertion_counter)
    assert_equal 1, assertion_counter.count
  end

  def test_should_not_increment_assertion_counter_for_stub_because_it_does_not_need_verifying
    stub = Expectation.new(nil, :expected_method).at_least(0)
    assertion_counter = SimpleCounter.new
    stub.verified?(assertion_counter)
    assert_equal 0, assertion_counter.count
  end

  def test_should_store_backtrace_from_point_where_expectation_was_created
    execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
    assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
  end

  class FakeMock

    def initialize(name)
      @name = name
    end

    def mocha_inspect
      @name
    end

  end

  def test_should_raise_error_with_message_indicating_which_method_was_expected_to_be_called_on_which_mock_object_with_which_parameters_and_in_what_sequences
    mock = FakeMock.new('mock')
    sequence_one = Sequence.new('one')
    sequence_two = Sequence.new('two')
    expectation = Expectation.new(mock, :expected_method).with(1, 2, {'a' => true}, {:b => false}, [1, 2, 3]).in_sequence(sequence_one, sequence_two)
    assert !expectation.verified?
    assert_match "mock.expected_method(1, 2, {'a' => true}, {:b => false}, [1, 2, 3]); in sequence 'one'; in sequence 'two'", expectation.mocha_inspect
  end

  class FakeConstraint

    def initialize(allows_invocation_now)
      @allows_invocation_now = allows_invocation_now
    end

    def allows_invocation_now?
      @allows_invocation_now
    end

  end

  def test_should_be_in_correct_order_if_all_ordering_constraints_allow_invocation_now
    constraint_one = FakeConstraint.new(allows_invocation_now = true)
    constraint_two = FakeConstraint.new(allows_invocation_now = true)
    expectation = Expectation.new(nil, :method_one)
    expectation.add_ordering_constraint(constraint_one)
    expectation.add_ordering_constraint(constraint_two)
    assert expectation.in_correct_order?
  end

  def test_should_not_be_in_correct_order_if_one_ordering_constraint_does_not_allow_invocation_now
    constraint_one = FakeConstraint.new(allows_invocation_now = true)
    constraint_two = FakeConstraint.new(allows_invocation_now = false)
    expectation = Expectation.new(nil, :method_one)
    expectation.add_ordering_constraint(constraint_one)
    expectation.add_ordering_constraint(constraint_two)
    assert !expectation.in_correct_order?
  end

  def test_should_match_if_all_ordering_constraints_allow_invocation_now
    constraint_one = FakeConstraint.new(allows_invocation_now = true)
    constraint_two = FakeConstraint.new(allows_invocation_now = true)
    expectation = Expectation.new(nil, :method_one)
    expectation.add_ordering_constraint(constraint_one)
    expectation.add_ordering_constraint(constraint_two)
    assert expectation.match?(:method_one)
  end

  def test_should_not_match_if_one_ordering_constraints_does_not_allow_invocation_now
    constraint_one = FakeConstraint.new(allows_invocation_now = true)
    constraint_two = FakeConstraint.new(allows_invocation_now = false)
    expectation = Expectation.new(nil, :method_one)
    expectation.add_ordering_constraint(constraint_one)
    expectation.add_ordering_constraint(constraint_two)
    assert !expectation.match?(:method_one)
  end

  def test_should_not_be_satisfied_when_required_invocation_has_not_been_made
    expectation = Expectation.new(nil, :method_one).times(1)
    assert !expectation.satisfied?
  end

  def test_should_be_satisfied_when_required_invocation_has_been_made
    expectation = Expectation.new(nil, :method_one).times(1)
    expectation.invoke
    assert expectation.satisfied?
  end

  def test_should_not_be_satisfied_when_minimum_number_of_invocations_has_not_been_made
    expectation = Expectation.new(nil, :method_one).at_least(2)
    expectation.invoke
    assert !expectation.satisfied?
  end

  def test_should_be_satisfied_when_minimum_number_of_invocations_has_been_made
    expectation = Expectation.new(nil, :method_one).at_least(2)
    2.times { expectation.invoke }
    assert expectation.satisfied?
  end

  class FakeSequence

    attr_reader :expectations

    def initialize
      @expectations = []
    end

    def constrain_as_next_in_sequence(expectation)
      @expectations << expectation
    end

  end

  def test_should_tell_sequences_to_constrain_expectation_as_next_in_sequence
    sequence_one = FakeSequence.new
    sequence_two = FakeSequence.new
    expectation = Expectation.new(nil, :method_one)
    assert_equal expectation, expectation.in_sequence(sequence_one, sequence_two)
    assert_equal [expectation], sequence_one.expectations
    assert_equal [expectation], sequence_two.expectations
  end

  class FakeState

    def initialize
      @active = false
    end

    def activate
      @active = true
    end

    def active?
      @active
    end

  end

  def test_should_change_state_when_expectation_is_invoked
    state = FakeState.new
    expectation = Expectation.new(nil, :method_one)

    expectation.then(state)

    expectation.invoke
    assert state.active?
  end

  def test_should_match_when_state_is_active
    state = FakeState.new
    expectation = Expectation.new(nil, :method_one)

    expectation.when(state)
    assert !expectation.match?(:method_one)

    state.activate
    assert expectation.match?(:method_one)
  end

end