mocha 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/Rakefile +3 -1
  2. data/examples/misc.rb +44 -36
  3. data/examples/stubba.rb +1 -1
  4. data/lib/mocha/auto_verify.rb +38 -31
  5. data/lib/mocha/central.rb +1 -1
  6. data/lib/mocha/class_method.rb +5 -1
  7. data/lib/mocha/expectation.rb +63 -61
  8. data/lib/mocha/expectation_error.rb +9 -0
  9. data/lib/mocha/expectation_list.rb +11 -10
  10. data/lib/mocha/method_matcher.rb +21 -0
  11. data/lib/mocha/missing_expectation.rb +5 -15
  12. data/lib/mocha/mock.rb +28 -26
  13. data/lib/mocha/parameter_matchers.rb +17 -1
  14. data/lib/mocha/parameter_matchers/all_of.rb +6 -3
  15. data/lib/mocha/parameter_matchers/any_of.rb +6 -3
  16. data/lib/mocha/parameter_matchers/any_parameters.rb +40 -0
  17. data/lib/mocha/parameter_matchers/anything.rb +5 -2
  18. data/lib/mocha/parameter_matchers/base.rb +15 -0
  19. data/lib/mocha/parameter_matchers/equals.rb +42 -0
  20. data/lib/mocha/parameter_matchers/has_entries.rb +42 -0
  21. data/lib/mocha/parameter_matchers/has_entry.rb +20 -4
  22. data/lib/mocha/parameter_matchers/has_key.rb +5 -2
  23. data/lib/mocha/parameter_matchers/has_value.rb +5 -2
  24. data/lib/mocha/parameter_matchers/includes.rb +5 -2
  25. data/lib/mocha/parameter_matchers/instance_of.rb +5 -2
  26. data/lib/mocha/parameter_matchers/is_a.rb +42 -0
  27. data/lib/mocha/parameter_matchers/kind_of.rb +5 -2
  28. data/lib/mocha/parameter_matchers/not.rb +42 -0
  29. data/lib/mocha/parameter_matchers/object.rb +9 -0
  30. data/lib/mocha/parameter_matchers/optionally.rb +33 -0
  31. data/lib/mocha/parameter_matchers/regexp_matches.rb +5 -2
  32. data/lib/mocha/parameters_matcher.rb +37 -0
  33. data/lib/mocha/pretty_parameters.rb +1 -1
  34. data/lib/mocha/return_values.rb +7 -4
  35. data/lib/mocha/sequence.rb +42 -0
  36. data/lib/mocha/yield_parameters.rb +3 -3
  37. data/test/acceptance/expected_invocation_count_acceptance_test.rb +8 -8
  38. data/test/acceptance/mock_with_initializer_block_acceptance_test.rb +44 -0
  39. data/test/acceptance/optional_parameters_acceptance_test.rb +63 -0
  40. data/test/acceptance/parameter_matcher_acceptance_test.rb +38 -2
  41. data/test/acceptance/partial_mocks_acceptance_test.rb +40 -0
  42. data/test/acceptance/sequence_acceptance_test.rb +179 -0
  43. data/test/integration/mocha_test_result_integration_test.rb +3 -3
  44. data/test/integration/stubba_integration_test.rb +2 -2
  45. data/test/integration/stubba_test_result_integration_test.rb +2 -2
  46. data/test/test_runner.rb +2 -2
  47. data/test/unit/any_instance_method_test.rb +2 -0
  48. data/test/unit/auto_verify_test.rb +10 -3
  49. data/test/unit/central_test.rb +1 -1
  50. data/test/unit/class_method_test.rb +4 -0
  51. data/test/unit/expectation_error_test.rb +24 -0
  52. data/test/unit/expectation_list_test.rb +6 -0
  53. data/test/unit/expectation_test.rb +111 -27
  54. data/test/unit/method_matcher_test.rb +23 -0
  55. data/test/unit/missing_expectation_test.rb +24 -27
  56. data/test/unit/mock_test.rb +29 -22
  57. data/test/unit/object_inspect_test.rb +4 -2
  58. data/test/unit/parameter_matchers/all_of_test.rb +2 -2
  59. data/test/unit/parameter_matchers/any_of_test.rb +2 -2
  60. data/test/unit/parameter_matchers/anything_test.rb +2 -2
  61. data/test/unit/parameter_matchers/has_entries_test.rb +30 -0
  62. data/test/unit/parameter_matchers/has_entry_test.rb +20 -5
  63. data/test/unit/parameter_matchers/has_key_test.rb +2 -2
  64. data/test/unit/parameter_matchers/has_value_test.rb +2 -2
  65. data/test/unit/parameter_matchers/includes_test.rb +2 -2
  66. data/test/unit/parameter_matchers/instance_of_test.rb +2 -2
  67. data/test/unit/parameter_matchers/is_a_test.rb +25 -0
  68. data/test/unit/parameter_matchers/kind_of_test.rb +3 -3
  69. data/test/unit/parameter_matchers/not_test.rb +26 -0
  70. data/test/unit/parameter_matchers/regexp_matches_test.rb +2 -2
  71. data/test/unit/parameter_matchers/stub_matcher.rb +2 -1
  72. data/test/unit/parameters_matcher_test.rb +121 -0
  73. data/test/unit/sequence_test.rb +104 -0
  74. metadata +35 -6
  75. data/test/unit/pretty_parameters_test.rb +0 -32
@@ -20,8 +20,8 @@ module TestRunner
20
20
  end
21
21
 
22
22
  def assert_passed(test_result)
23
- flunk "Test failed unexpectedly with message: #{test_result.failures.first.message}" if test_result.failure_count > 0
24
- flunk "Test failed unexpectedly with message: #{test_result.errors.first.message}" if test_result.error_count > 0
23
+ flunk "Test failed unexpectedly with message: #{test_result.failures}" if test_result.failure_count > 0
24
+ flunk "Test failed unexpectedly with message: #{test_result.errors}" if test_result.error_count > 0
25
25
  end
26
26
 
27
27
  def assert_failed(test_result)
@@ -36,6 +36,7 @@ class AnyInstanceMethodTest < Test::Unit::TestCase
36
36
  any_instance.define_instance_method(:mocha) { mocha }
37
37
  klass.define_instance_method(:any_instance) { any_instance }
38
38
 
39
+ method.hide_original_method
39
40
  method.define_new_method
40
41
 
41
42
  instance = klass.new
@@ -51,6 +52,7 @@ class AnyInstanceMethodTest < Test::Unit::TestCase
51
52
  hidden_method_x = method.hidden_method.to_sym
52
53
  klass.send(:define_method, hidden_method_x, Proc.new { :original_result })
53
54
 
55
+ method.remove_new_method
54
56
  method.restore_original_method
55
57
 
56
58
  instance = klass.new
@@ -9,7 +9,6 @@ class AutoVerifyTest < Test::Unit::TestCase
9
9
  def setup
10
10
  @test_case = Object.new
11
11
  class << test_case
12
- def self.add_teardown_method(symbol); end
13
12
  include Mocha::AutoVerify
14
13
  end
15
14
  end
@@ -38,7 +37,7 @@ class AutoVerifyTest < Test::Unit::TestCase
38
37
 
39
38
  def test_should_build_stub_that_stubs_all_methods
40
39
  stub = test_case.stub_everything
41
- assert stub.stub_everything
40
+ assert stub.everything_stubbed
42
41
  end
43
42
 
44
43
  def test_should_add_expectations_to_stub_that_stubs_all_methods
@@ -78,7 +77,11 @@ class AutoVerifyTest < Test::Unit::TestCase
78
77
  mocks = Array.new(3) do
79
78
  mock = Object.new
80
79
  mock.define_instance_accessor(:verify_called)
81
- mock.define_instance_method(:verify) { self.verify_called = true }
80
+ class << mock
81
+ def verify(&block)
82
+ self.verify_called = true
83
+ end
84
+ end
82
85
  mock
83
86
  end
84
87
  test_case.replace_instance_method(:mocks) { mocks }
@@ -119,4 +122,8 @@ class AutoVerifyTest < Test::Unit::TestCase
119
122
  assert_equal '#<Mock:named_stub>', stub.mocha_inspect
120
123
  end
121
124
 
125
+ def test_should_build_sequence
126
+ assert_not_nil test_case.sequence('name')
127
+ end
128
+
122
129
  end
@@ -72,7 +72,7 @@ class CentralTest < Test::Unit::TestCase
72
72
  stubba = Central.new
73
73
  stubba.stubba_methods = [method_1, method_2]
74
74
 
75
- assert_equal 2, stubba.unique_mocks.size
75
+ assert_equal 2, stubba.unique_mocks.length
76
76
  assert stubba.unique_mocks.include?(:mock_1)
77
77
  assert stubba.unique_mocks.include?(:mock_2)
78
78
  end
@@ -90,6 +90,7 @@ class ClassMethodTest < Test::Unit::TestCase
90
90
  mocha.expects(:method_x).with(:param1, :param2).returns(:result)
91
91
  method = ClassMethod.new(klass, :method_x)
92
92
 
93
+ method.hide_original_method
93
94
  method.define_new_method
94
95
  result = klass.method_x(:param1, :param2)
95
96
 
@@ -112,6 +113,7 @@ class ClassMethodTest < Test::Unit::TestCase
112
113
  hidden_method_x = method.hidden_method.to_sym
113
114
  klass.define_instance_method(hidden_method_x) { :original_result }
114
115
 
116
+ method.remove_new_method
115
117
  method.restore_original_method
116
118
 
117
119
  assert_equal :original_result, klass.method_x
@@ -130,6 +132,7 @@ class ClassMethodTest < Test::Unit::TestCase
130
132
  def test_should_call_hide_original_method
131
133
  klass = Class.new { def self.method_x; end }
132
134
  method = ClassMethod.new(klass, :method_x)
135
+ method.hide_original_method
133
136
  method.define_instance_accessor(:hide_called)
134
137
  method.replace_instance_method(:hide_original_method) { self.hide_called = true }
135
138
 
@@ -189,6 +192,7 @@ class ClassMethodTest < Test::Unit::TestCase
189
192
  mocha = Object.new
190
193
  stubbee = Object.new
191
194
  stubbee.define_instance_accessor(:mocha) { mocha }
195
+ stubbee.mocha = nil
192
196
  method = ClassMethod.new(stubbee, :method_name)
193
197
  assert_equal stubbee.mocha, method.mock
194
198
  end
@@ -0,0 +1,24 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha/expectation_error'
3
+
4
+ class ExpectationErrorTest < Test::Unit::TestCase
5
+
6
+ include Mocha
7
+
8
+ def test_should_exclude_mocha_locations_from_backtrace
9
+ mocha_lib = "/username/workspace/mocha_wibble/lib/"
10
+ backtrace = [ mocha_lib + 'exclude/me/1', mocha_lib + 'exclude/me/2', '/keep/me', mocha_lib + 'exclude/me/3']
11
+ expectation_error = ExpectationError.new(nil, backtrace, mocha_lib)
12
+ assert_equal ['/keep/me'], expectation_error.backtrace
13
+ end
14
+
15
+ def test_should_determine_path_for_mocha_lib_directory
16
+ assert_match Regexp.new("/lib/$"), ExpectationError::LIB_DIRECTORY
17
+ end
18
+
19
+ def test_should_set_error_message
20
+ expectation_error = ExpectationError.new('message')
21
+ assert_equal 'message', expectation_error.message
22
+ end
23
+
24
+ end
@@ -8,6 +8,12 @@ class ExpectationListTest < Test::Unit::TestCase
8
8
 
9
9
  include Mocha
10
10
 
11
+ def test_should_return_added_expectation
12
+ expectation_list = ExpectationList.new
13
+ expectation = Expectation.new(nil, :my_method)
14
+ assert_same expectation, expectation_list.add(expectation)
15
+ end
16
+
11
17
  def test_should_find_matching_expectation
12
18
  expectation_list = ExpectationList.new
13
19
  expectation1 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
@@ -1,6 +1,7 @@
1
1
  require File.join(File.dirname(__FILE__), "..", "test_helper")
2
2
  require 'method_definer'
3
3
  require 'mocha/expectation'
4
+ require 'mocha/sequence'
4
5
  require 'execution_point'
5
6
  require 'deprecation_disabler'
6
7
 
@@ -357,43 +358,126 @@ class ExpectationTest < Test::Unit::TestCase
357
358
  assert_equal execution_point, ExecutionPoint.new(error.backtrace)
358
359
  end
359
360
 
360
- def test_should_display_expectation_message_in_exception_message
361
+ def test_should_display_expectation_in_exception_message
361
362
  options = [:a, :b, {:c => 1, :d => 2}]
362
363
  expectation = new_expectation.with(*options)
363
- exception = assert_raise(ExpectationError) {
364
- expectation.verify
365
- }
364
+ exception = assert_raise(ExpectationError) { expectation.verify }
366
365
  assert exception.message.include?(expectation.method_signature)
367
366
  end
368
367
 
369
- def test_should_combine_method_name_and_pretty_parameters
370
- arguments = 1, 2, {'a' => true, :b => false}, [1, 2, 3]
371
- expectation = new_expectation.with(*arguments)
372
- assert_equal "expected_method(#{PrettyParameters.new(arguments).pretty})", expectation.method_signature
373
- end
374
-
375
- def test_should_not_include_parameters_in_message
376
- assert_equal "expected_method", new_expectation.method_signature
368
+ class FakeMock
369
+
370
+ def initialize(name)
371
+ @name = name
372
+ end
373
+
374
+ def mocha_inspect
375
+ @name
376
+ end
377
+
377
378
  end
378
379
 
379
- def test_should_raise_error_with_message_indicating_which_method_was_expected_to_be_called_on_which_mock_object
380
- mock = Class.new { def mocha_inspect; 'mock'; end }.new
381
- expectation = Expectation.new(mock, :expected_method)
380
+ 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
381
+ mock = FakeMock.new('mock')
382
+ sequence_one = Sequence.new('one')
383
+ sequence_two = Sequence.new('two')
384
+ expectation = Expectation.new(mock, :expected_method).with(1, 2, {'a' => true, :b => false}, [1, 2, 3]).in_sequence(sequence_one, sequence_two)
382
385
  e = assert_raise(ExpectationError) { expectation.verify }
383
- assert_match "mock.expected_method", e.message
386
+ assert_match "mock.expected_method(1, 2, {'a' => true, :b => false}, [1, 2, 3]); in sequence 'one'; in sequence 'two'", e.message
387
+ end
388
+
389
+ class FakeConstraint
390
+
391
+ def initialize(allows_invocation_now)
392
+ @allows_invocation_now = allows_invocation_now
393
+ end
394
+
395
+ def allows_invocation_now?
396
+ @allows_invocation_now
397
+ end
398
+
399
+ end
400
+
401
+ def test_should_be_in_correct_order_if_all_ordering_constraints_allow_invocation_now
402
+ constraint_one = FakeConstraint.new(allows_invocation_now = true)
403
+ constraint_two = FakeConstraint.new(allows_invocation_now = true)
404
+ expectation = Expectation.new(nil, :method_one)
405
+ expectation.add_ordering_constraint(constraint_one)
406
+ expectation.add_ordering_constraint(constraint_two)
407
+ assert expectation.in_correct_order?
408
+ end
409
+
410
+ def test_should_not_be_in_correct_order_if_one_ordering_constraint_does_not_allow_invocation_now
411
+ constraint_one = FakeConstraint.new(allows_invocation_now = true)
412
+ constraint_two = FakeConstraint.new(allows_invocation_now = false)
413
+ expectation = Expectation.new(nil, :method_one)
414
+ expectation.add_ordering_constraint(constraint_one)
415
+ expectation.add_ordering_constraint(constraint_two)
416
+ assert !expectation.in_correct_order?
417
+ end
418
+
419
+ def test_should_match_if_all_ordering_constraints_allow_invocation_now
420
+ constraint_one = FakeConstraint.new(allows_invocation_now = true)
421
+ constraint_two = FakeConstraint.new(allows_invocation_now = true)
422
+ expectation = Expectation.new(nil, :method_one)
423
+ expectation.add_ordering_constraint(constraint_one)
424
+ expectation.add_ordering_constraint(constraint_two)
425
+ assert expectation.match?(:method_one)
384
426
  end
385
-
386
- def test_should_exclude_mocha_locations_from_backtrace
387
- mocha_lib = "/username/workspace/mocha_wibble/lib/"
388
- backtrace = [ mocha_lib + 'exclude/me/1', mocha_lib + 'exclude/me/2', '/keep/me', mocha_lib + 'exclude/me/3']
389
- expectation = Expectation.new(nil, :expected_method, backtrace)
390
- expectation.define_instance_method(:mocha_lib_directory) { mocha_lib }
391
- assert_equal ['/keep/me'], expectation.filtered_backtrace
427
+
428
+ def test_should_not_match_if_one_ordering_constraints_does_not_allow_invocation_now
429
+ constraint_one = FakeConstraint.new(allows_invocation_now = true)
430
+ constraint_two = FakeConstraint.new(allows_invocation_now = false)
431
+ expectation = Expectation.new(nil, :method_one)
432
+ expectation.add_ordering_constraint(constraint_one)
433
+ expectation.add_ordering_constraint(constraint_two)
434
+ assert !expectation.match?(:method_one)
392
435
  end
393
-
394
- def test_should_determine_path_for_mocha_lib_directory
395
- expectation = new_expectation()
396
- assert_match Regexp.new("/lib/$"), expectation.mocha_lib_directory
436
+
437
+ def test_should_not_be_satisfied_when_required_invocation_has_not_been_made
438
+ expectation = Expectation.new(nil, :method_one).times(1)
439
+ assert !expectation.satisfied?
440
+ end
441
+
442
+ def test_should_be_satisfied_when_required_invocation_has_been_made
443
+ expectation = Expectation.new(nil, :method_one).times(1)
444
+ expectation.invoke
445
+ assert expectation.satisfied?
446
+ end
447
+
448
+ def test_should_not_be_satisfied_when_minimum_number_of_invocations_has_not_been_made
449
+ expectation = Expectation.new(nil, :method_one).at_least(2)
450
+ expectation.invoke
451
+ assert !expectation.satisfied?
452
+ end
453
+
454
+ def test_should_be_satisfied_when_minimum_number_of_invocations_has_been_made
455
+ expectation = Expectation.new(nil, :method_one).at_least(2)
456
+ 2.times { expectation.invoke }
457
+ assert expectation.satisfied?
458
+ end
459
+
460
+ class FakeSequence
461
+
462
+ attr_reader :expectations
463
+
464
+ def initialize
465
+ @expectations = []
466
+ end
467
+
468
+ def constrain_as_next_in_sequence(expectation)
469
+ @expectations << expectation
470
+ end
471
+
472
+ end
473
+
474
+ def test_should_tell_sequences_to_constrain_expectation_as_next_in_sequence
475
+ sequence_one = FakeSequence.new
476
+ sequence_two = FakeSequence.new
477
+ expectation = Expectation.new(nil, :method_one)
478
+ assert_equal expectation, expectation.in_sequence(sequence_one, sequence_two)
479
+ assert_equal [expectation], sequence_one.expectations
480
+ assert_equal [expectation], sequence_two.expectations
397
481
  end
398
482
 
399
483
  end
@@ -0,0 +1,23 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha/method_matcher'
3
+
4
+ class MethodMatcherTest < Test::Unit::TestCase
5
+
6
+ include Mocha
7
+
8
+ def test_should_match_if_actual_method_name_is_same_as_expected_method_name
9
+ method_matcher = MethodMatcher.new(:method_name)
10
+ assert method_matcher.match?(:method_name)
11
+ end
12
+
13
+ def test_should_not_match_if_actual_method_name_is_not_same_as_expected_method_name
14
+ method_matcher = MethodMatcher.new(:method_name)
15
+ assert !method_matcher.match?(:different_method_name)
16
+ end
17
+
18
+ def test_should_describe_what_method_is_expected
19
+ method_matcher = MethodMatcher.new(:method_name)
20
+ assert_equal "method_name", method_matcher.mocha_inspect
21
+ end
22
+
23
+ end
@@ -1,45 +1,42 @@
1
1
  require File.join(File.dirname(__FILE__), "..", "test_helper")
2
2
 
3
3
  require 'mocha/missing_expectation'
4
- require 'method_definer'
4
+ require 'mocha/mock'
5
5
 
6
- class MissingExpectationsTest < Test::Unit::TestCase
6
+ class MissingExpectationTest < Test::Unit::TestCase
7
7
 
8
8
  include Mocha
9
9
 
10
- def test_should_find_similar_expectations_on_mock
11
- mock = Object.new
12
- missing_expectation = MissingExpectation.new(mock, :expected_method)
13
- method_names = []
14
- similar_expectations = [Expectation.new(mock, :expected_method)]
15
- mock.define_instance_method(:similar_expectations) { |method_name| method_names << method_name; similar_expectations }
16
- assert_equal similar_expectations, missing_expectation.similar_expectations
17
- assert_equal [:expected_method], method_names
18
- end
19
-
20
10
  def test_should_report_similar_expectations
21
- expectation_1 = Expectation.new(nil, :expected_method).with(1)
22
- expectation_2 = Expectation.new(nil, :expected_method).with(2)
23
-
24
- mock = Object.new
25
- mock.define_instance_method(:similar_expectations) { [expectation_1, expectation_2] }
26
- mock.define_instance_method(:mocha_inspect) { 'mocha_inspect' }
11
+ mock = Mock.new
12
+ expectation_1 = mock.expects(:method_one).with(1)
13
+ expectation_2 = mock.expects(:method_one).with(1, 1)
14
+ expectation_3 = mock.expects(:method_two).with(2)
27
15
 
28
- missing_expectation = MissingExpectation.new(mock, :expected_method).with(3)
29
-
16
+ missing_expectation = MissingExpectation.new(mock, :method_one)
30
17
  exception = assert_raise(ExpectationError) { missing_expectation.verify }
31
- assert_equal "mocha_inspect.expected_method(3) - expected calls: 0, actual calls: 1\nSimilar expectations:\nexpected_method(1)\nexpected_method(2)", exception.message
18
+
19
+ expected_message = [
20
+ "#{missing_expectation.error_message(0, 1)}",
21
+ "Similar expectations:",
22
+ "#{expectation_1.method_signature}",
23
+ "#{expectation_2.method_signature}"
24
+ ].join("\n")
25
+
26
+ assert_equal expected_message, exception.message
32
27
  end
33
28
 
34
29
  def test_should_not_report_similar_expectations_if_there_are_none
35
- mock = Object.new
36
- mock.define_instance_method(:similar_expectations) { [] }
37
- mock.define_instance_method(:mocha_inspect) { 'mocha_inspect' }
30
+ mock = Mock.new
31
+ mock.expects(:method_two).with(2)
32
+ mock.expects(:method_two).with(2, 2)
38
33
 
39
- missing_expectation = MissingExpectation.new(mock, :unexpected_method).with(1)
40
-
34
+ missing_expectation = MissingExpectation.new(mock, :method_one)
41
35
  exception = assert_raise(ExpectationError) { missing_expectation.verify }
42
- assert_equal "mocha_inspect.unexpected_method(1) - expected calls: 0, actual calls: 1", exception.message
36
+
37
+ expected_message = "#{missing_expectation.error_message(0, 1)}"
38
+
39
+ assert_equal expected_message, exception.message
43
40
  end
44
41
 
45
42
  end
@@ -24,31 +24,32 @@ class MockTest < Test::Unit::TestCase
24
24
 
25
25
  def test_should_not_stub_everything_by_default
26
26
  mock = Mock.new
27
- assert_equal false, mock.stub_everything
27
+ assert_equal false, mock.everything_stubbed
28
28
  end
29
29
 
30
30
  def test_should_stub_everything
31
- mock = Mock.new(stub_everything = true)
32
- assert_equal true, mock.stub_everything
31
+ mock = Mock.new
32
+ mock.stub_everything
33
+ assert_equal true, mock.everything_stubbed
33
34
  end
34
35
 
35
36
  def test_should_display_object_id_for_mocha_inspect_if_mock_has_no_name
36
37
  mock = Mock.new
37
- assert_match Regexp.new("^#<Mock:0x[0-9A-Fa-f]{1,8}>$"), mock.mocha_inspect
38
+ assert_match Regexp.new("^#<Mock:0x[0-9A-Fa-f]{1,12}>$"), mock.mocha_inspect
38
39
  end
39
40
 
40
41
  def test_should_display_name_for_mocha_inspect_if_mock_has_name
41
- mock = Mock.new(false, 'named_mock')
42
+ mock = Mock.new('named_mock')
42
43
  assert_equal "#<Mock:named_mock>", mock.mocha_inspect
43
44
  end
44
45
 
45
46
  def test_should_display_object_id_for_inspect_if_mock_has_no_name
46
47
  mock = Mock.new
47
- assert_match Regexp.new("^#<Mock:0x[0-9A-Fa-f]{1,8}>$"), mock.inspect
48
+ assert_match Regexp.new("^#<Mock:0x[0-9A-Fa-f]{1,12}>$"), mock.inspect
48
49
  end
49
50
 
50
51
  def test_should_display_name_for_inspect_if_mock_has_name
51
- mock = Mock.new(false, 'named_mock')
52
+ mock = Mock.new('named_mock')
52
53
  assert_equal "#<Mock:named_mock>", mock.inspect
53
54
  end
54
55
 
@@ -61,20 +62,24 @@ class MockTest < Test::Unit::TestCase
61
62
  mock = Mock.new
62
63
  assert_equal true, mock.eql?(mock)
63
64
  end
64
-
65
+
66
+ if RUBY_VERSION < '1.9'
67
+ OBJECT_METHODS = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject { |m| m =~ /^__.*__$/ }
68
+ else
69
+ OBJECT_METHODS = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject { |m| m =~ /^__.*__$/ || m == :object_id }
70
+ end
71
+
65
72
  def test_should_be_able_to_mock_standard_object_methods
66
73
  mock = Mock.new
67
- object_methods = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject { |m| m =~ /^__.*__$/ }.sort
68
- object_methods.each { |method| mock.__expects__(method.to_sym).returns(method) }
69
- object_methods.each { |method| assert_equal method, mock.__send__(method.to_sym) }
74
+ OBJECT_METHODS.each { |method| mock.__expects__(method.to_sym).returns(method) }
75
+ OBJECT_METHODS.each { |method| assert_equal method, mock.__send__(method.to_sym) }
70
76
  assert_nothing_raised(ExpectationError) { mock.verify }
71
77
  end
72
-
78
+
73
79
  def test_should_be_able_to_stub_standard_object_methods
74
80
  mock = Mock.new
75
- object_methods = STANDARD_OBJECT_PUBLIC_INSTANCE_METHODS.reject { |m| m =~ /^__.*__$/ }.sort
76
- object_methods.each { |method| mock.__stubs__(method.to_sym).returns(method) }
77
- object_methods.each { |method| assert_equal method, mock.__send__(method.to_sym) }
81
+ OBJECT_METHODS.each { |method| mock.__stubs__(method.to_sym).returns(method) }
82
+ OBJECT_METHODS.each { |method| assert_equal method, mock.__send__(method.to_sym) }
78
83
  end
79
84
 
80
85
  def test_should_create_and_add_expectations
@@ -113,7 +118,8 @@ class MockTest < Test::Unit::TestCase
113
118
  end
114
119
 
115
120
  def test_should_not_raise_error_if_stubbing_everything
116
- mock = Mock.new(stub_everything = true)
121
+ mock = Mock.new
122
+ mock.stub_everything
117
123
  result = nil
118
124
  assert_nothing_raised(ExpectationError) do
119
125
  result = mock.unexpected_method
@@ -158,7 +164,7 @@ class MockTest < Test::Unit::TestCase
158
164
  mock = Mock.new
159
165
  mock.expects(:expected_method).with(1)
160
166
  exception = assert_raise(ExpectationError) { mock.expected_method(2) }
161
- assert_equal "#{mock.mocha_inspect}.expected_method(2) - expected calls: 0, actual calls: 1\nSimilar expectations:\nexpected_method(1)", exception.message
167
+ assert_equal "#{mock.mocha_inspect}.expected_method(2) - expected calls: 0, actual calls: 1\nSimilar expectations:\n#{mock.mocha_inspect}.expected_method(1)", exception.message
162
168
  end
163
169
 
164
170
  def test_should_pass_block_through_to_expectations_verify_method
@@ -248,7 +254,7 @@ class MockTest < Test::Unit::TestCase
248
254
 
249
255
  def test_should_respond_to_methods_which_the_responder_does_responds_to
250
256
  instance = Class.new do
251
- define_method(:respond_to?) { true }
257
+ define_method(:respond_to?) { |symbol| true }
252
258
  end.new
253
259
  mock = Mock.new
254
260
  mock.responds_like(instance)
@@ -257,7 +263,7 @@ class MockTest < Test::Unit::TestCase
257
263
 
258
264
  def test_should_not_respond_to_methods_which_the_responder_does_not_responds_to
259
265
  instance = Class.new do
260
- define_method(:respond_to?) { false }
266
+ define_method(:respond_to?) { |symbol| false }
261
267
  end.new
262
268
  mock = Mock.new
263
269
  mock.responds_like(instance)
@@ -280,7 +286,7 @@ class MockTest < Test::Unit::TestCase
280
286
 
281
287
  def test_should_not_raise_no_method_error_if_responder_does_respond_to_invoked_method
282
288
  instance = Class.new do
283
- define_method(:respond_to?) { true }
289
+ define_method(:respond_to?) { |symbol| true }
284
290
  end.new
285
291
  mock = Mock.new
286
292
  mock.responds_like(instance)
@@ -290,7 +296,7 @@ class MockTest < Test::Unit::TestCase
290
296
 
291
297
  def test_should_raise_no_method_error_if_responder_does_not_respond_to_invoked_method
292
298
  instance = Class.new do
293
- define_method(:respond_to?) { false }
299
+ define_method(:respond_to?) { |symbol| false }
294
300
  define_method(:mocha_inspect) { 'mocha_inspect' }
295
301
  end.new
296
302
  mock = Mock.new
@@ -301,7 +307,7 @@ class MockTest < Test::Unit::TestCase
301
307
 
302
308
  def test_should_raise_no_method_error_with_message_indicating_that_mock_is_constrained_to_respond_like_responder
303
309
  instance = Class.new do
304
- define_method(:respond_to?) { false }
310
+ define_method(:respond_to?) { |symbol| false }
305
311
  define_method(:mocha_inspect) { 'mocha_inspect' }
306
312
  end.new
307
313
  mock = Mock.new
@@ -313,4 +319,5 @@ class MockTest < Test::Unit::TestCase
313
319
  assert_match(/which responds like mocha_inspect/, e.message)
314
320
  end
315
321
  end
322
+
316
323
  end