mocha 1.10.0 → 1.11.2

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.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +1 -0
  3. data/.yardopts +1 -0
  4. data/RELEASE.md +49 -0
  5. data/Rakefile +1 -4
  6. data/docs/Mocha.html +2 -2
  7. data/docs/Mocha/API.html +167 -35
  8. data/docs/Mocha/ClassMethods.html +2 -2
  9. data/docs/Mocha/Configuration.html +197 -55
  10. data/docs/Mocha/Expectation.html +284 -76
  11. data/docs/Mocha/ExpectationError.html +2 -2
  12. data/docs/Mocha/ExpectationErrorFactory.html +2 -2
  13. data/docs/Mocha/Hooks.html +2 -2
  14. data/docs/Mocha/Integration.html +2 -2
  15. data/docs/Mocha/Integration/MiniTest.html +2 -2
  16. data/docs/Mocha/Integration/MiniTest/Adapter.html +2 -2
  17. data/docs/Mocha/Integration/TestUnit.html +2 -2
  18. data/docs/Mocha/Integration/TestUnit/Adapter.html +2 -2
  19. data/docs/Mocha/Mock.html +14 -41
  20. data/docs/Mocha/ObjectMethods.html +2 -2
  21. data/docs/Mocha/ParameterMatchers.html +14 -26
  22. data/docs/Mocha/ParameterMatchers/AllOf.html +2 -2
  23. data/docs/Mocha/ParameterMatchers/AnyOf.html +2 -2
  24. data/docs/Mocha/ParameterMatchers/AnyParameters.html +2 -2
  25. data/docs/Mocha/ParameterMatchers/Anything.html +2 -2
  26. data/docs/Mocha/ParameterMatchers/Base.html +2 -2
  27. data/docs/Mocha/ParameterMatchers/Equals.html +2 -2
  28. data/docs/Mocha/ParameterMatchers/EquivalentUri.html +2 -2
  29. data/docs/Mocha/ParameterMatchers/HasEntries.html +2 -2
  30. data/docs/Mocha/ParameterMatchers/HasEntry.html +2 -2
  31. data/docs/Mocha/ParameterMatchers/HasKey.html +2 -2
  32. data/docs/Mocha/ParameterMatchers/HasValue.html +2 -2
  33. data/docs/Mocha/ParameterMatchers/Includes.html +2 -2
  34. data/docs/Mocha/ParameterMatchers/InstanceOf.html +2 -2
  35. data/docs/Mocha/ParameterMatchers/IsA.html +2 -2
  36. data/docs/Mocha/ParameterMatchers/KindOf.html +2 -2
  37. data/docs/Mocha/ParameterMatchers/Not.html +2 -2
  38. data/docs/Mocha/ParameterMatchers/Optionally.html +2 -2
  39. data/docs/Mocha/ParameterMatchers/RegexpMatches.html +2 -2
  40. data/docs/Mocha/ParameterMatchers/RespondsWith.html +2 -2
  41. data/docs/Mocha/ParameterMatchers/YamlEquivalent.html +2 -2
  42. data/docs/Mocha/Sequence.html +2 -2
  43. data/docs/Mocha/StateMachine.html +5 -5
  44. data/docs/Mocha/StateMachine/State.html +2 -2
  45. data/docs/Mocha/StateMachine/StatePredicate.html +2 -2
  46. data/docs/Mocha/StubbingError.html +2 -2
  47. data/docs/_index.html +3 -3
  48. data/docs/file.COPYING.html +2 -2
  49. data/docs/file.MIT-LICENSE.html +2 -2
  50. data/docs/file.README.html +2 -2
  51. data/docs/file.RELEASE.html +74 -2
  52. data/docs/frames.html +1 -1
  53. data/docs/index.html +2 -2
  54. data/docs/method_list.html +51 -27
  55. data/docs/top-level-namespace.html +2 -2
  56. data/lib/mocha/api.rb +83 -19
  57. data/lib/mocha/block_matcher.rb +31 -0
  58. data/lib/mocha/configuration.rb +57 -1
  59. data/lib/mocha/deprecation.rb +2 -1
  60. data/lib/mocha/expectation.rb +48 -7
  61. data/lib/mocha/invocation.rb +20 -4
  62. data/lib/mocha/mock.rb +42 -16
  63. data/lib/mocha/mockery.rb +14 -14
  64. data/lib/mocha/parameter_matchers/has_entries.rb +2 -3
  65. data/lib/mocha/parameter_matchers/has_entry.rb +2 -3
  66. data/lib/mocha/parameter_matchers/has_key.rb +2 -3
  67. data/lib/mocha/parameter_matchers/has_value.rb +2 -3
  68. data/lib/mocha/parameter_matchers/is_a.rb +2 -3
  69. data/lib/mocha/parameter_matchers/not.rb +2 -3
  70. data/lib/mocha/state_machine.rb +2 -3
  71. data/lib/mocha/stubbed_method.rb +4 -6
  72. data/lib/mocha/version.rb +1 -1
  73. data/lib/mocha/yield_parameters.rb +5 -11
  74. data/test/acceptance/acceptance_test_helper.rb +1 -0
  75. data/test/acceptance/display_matching_invocations_alongside_expectations_test.rb +5 -5
  76. data/test/acceptance/failure_messages_test.rb +16 -0
  77. data/test/acceptance/issue_457_test.rb +31 -0
  78. data/test/acceptance/mocha_example_test.rb +11 -1
  79. data/test/acceptance/mock_built_with_first_argument_type_being_string_test.rb +98 -0
  80. data/test/acceptance/mock_test.rb +64 -12
  81. data/test/acceptance/multiple_yielding_test.rb +59 -0
  82. data/test/acceptance/stubba_example_test.rb +11 -1
  83. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +16 -0
  84. data/test/acceptance/yielding_test.rb +80 -0
  85. data/test/unit/expectation_test.rb +30 -3
  86. data/test/unit/yield_parameters_test.rb +35 -53
  87. metadata +8 -9
  88. data/lib/mocha/multiple_yields.rb +0 -15
  89. data/lib/mocha/no_yields.rb +0 -5
  90. data/lib/mocha/pretty_parameters.rb +0 -24
  91. data/lib/mocha/single_yield.rb +0 -13
  92. data/test/unit/multiple_yields_test.rb +0 -16
  93. data/test/unit/no_yields_test.rb +0 -16
  94. data/test/unit/single_yield_test.rb +0 -16
@@ -20,11 +20,10 @@ module Mocha
20
20
  # object.expects(:method_1).with(has_key('key_1'))
21
21
  # object.method_1('key_2' => 2)
22
22
  # # error raised, because method_1 was not called with Hash containing key: 'key_1'
23
- # rubocop:disable Naming/PredicateName
24
- def has_key(key)
23
+ #
24
+ def has_key(key) # rubocop:disable Naming/PredicateName
25
25
  HasKey.new(key)
26
26
  end
27
- # rubocop:enable Naming/PredicateName
28
27
 
29
28
  # Parameter matcher which matches when actual parameter contains +Hash+ entry with expected key.
30
29
  class HasKey < Base
@@ -20,11 +20,10 @@ module Mocha
20
20
  # object.expects(:method_1).with(has_value(1))
21
21
  # object.method_1('key_2' => 2)
22
22
  # # error raised, because method_1 was not called with Hash containing value: 1
23
- # rubocop:disable Naming/PredicateName
24
- def has_value(value)
23
+ #
24
+ def has_value(value) # rubocop:disable Naming/PredicateName
25
25
  HasValue.new(value)
26
26
  end
27
- # rubocop:enable Naming/PredicateName
28
27
 
29
28
  # Parameter matcher which matches when actual parameter contains +Hash+ entry with expected value.
30
29
  class HasValue < Base
@@ -21,11 +21,10 @@ module Mocha
21
21
  # object.expects(:method_1).with(is_a(Integer))
22
22
  # object.method_1('string')
23
23
  # # error raised, because method_1 was not called with an Integer
24
- # rubocop:disable Naming/PredicateName
25
- def is_a(klass)
24
+ #
25
+ def is_a(klass) # rubocop:disable Naming/PredicateName
26
26
  IsA.new(klass)
27
27
  end
28
- # rubocop:enable Naming/PredicateName
29
28
 
30
29
  # Parameter matcher which matches when actual parameter is a specific class.
31
30
  class IsA < Base
@@ -20,11 +20,10 @@ module Mocha
20
20
  # object.expects(:method_1).with(Not(includes(1)))
21
21
  # object.method_1([0, 1, 2, 3])
22
22
  # # error raised, because method_1 was not called with object not including 1
23
- # rubocop:disable Naming/MethodName
24
- def Not(matcher)
23
+ #
24
+ def Not(matcher) # rubocop:disable Naming/MethodName
25
25
  Not.new(matcher)
26
26
  end
27
- # rubocop:enable Naming/MethodName
28
27
 
29
28
  # Parameter matcher which inverts the logic of the specified matcher using a logical NOT operation.
30
29
  class Not < Base
@@ -84,11 +84,10 @@ module Mocha
84
84
  end
85
85
 
86
86
  # Provides a mechanism to determine whether the {StateMachine} is not in the state specified by +state_name+ at some point in the future.
87
- # rubocop:disable Naming/PredicateName
88
- def is_not(state_name)
87
+ #
88
+ def is_not(state_name) # rubocop:disable Naming/PredicateName
89
89
  StatePredicate.new(self, state_name)
90
90
  end
91
- # rubocop:enable Naming/PredicateName
92
91
 
93
92
  # @private
94
93
  def mocha_inspect
@@ -74,7 +74,7 @@ module Mocha
74
74
  def restore_original_method
75
75
  return if use_prepended_module_for_stub_method?
76
76
  if stub_method_overwrites_original_method?
77
- original_method_owner.send(:define_method, method_name, method_body(original_method))
77
+ original_method_owner.send(:define_method, method_name, method_body(@original_method))
78
78
  end
79
79
  retain_original_visibility(original_method_owner)
80
80
  end
@@ -93,18 +93,16 @@ module Mocha
93
93
  private
94
94
 
95
95
  def retain_original_visibility(method_owner)
96
- return unless original_visibility
97
- Module.instance_method(original_visibility).bind(method_owner).call(method_name)
96
+ return unless @original_visibility
97
+ Module.instance_method(@original_visibility).bind(method_owner).call(method_name)
98
98
  end
99
99
 
100
- attr_reader :original_method, :original_visibility
101
-
102
100
  def store_original_method_visibility
103
101
  @original_visibility = original_method_owner.__method_visibility__(method_name)
104
102
  end
105
103
 
106
104
  def stub_method_overwrites_original_method?
107
- original_method && original_method.owner == original_method_owner
105
+ @original_method && @original_method.owner == original_method_owner
108
106
  end
109
107
 
110
108
  def remove_original_method_from_stubbee
@@ -1,3 +1,3 @@
1
1
  module Mocha
2
- VERSION = '1.10.0'.freeze
2
+ VERSION = '1.11.2'.freeze
3
3
  end
@@ -1,7 +1,3 @@
1
- require 'mocha/no_yields'
2
- require 'mocha/single_yield'
3
- require 'mocha/multiple_yields'
4
-
5
1
  module Mocha
6
2
  class YieldParameters
7
3
  def initialize
@@ -10,18 +6,16 @@ module Mocha
10
6
 
11
7
  def next_invocation
12
8
  case @parameter_groups.length
13
- when 0 then NoYields.new
9
+ when 0 then []
14
10
  when 1 then @parameter_groups.first
15
11
  else @parameter_groups.shift
16
12
  end
17
13
  end
18
14
 
19
- def add(*parameters)
20
- @parameter_groups << SingleYield.new(*parameters)
21
- end
22
-
23
- def multiple_add(*parameter_groups)
24
- @parameter_groups << MultipleYields.new(*parameter_groups)
15
+ def add(*parameter_groups)
16
+ @parameter_groups << parameter_groups.map do |pg|
17
+ pg.is_a?(Array) ? pg : [pg]
18
+ end
25
19
  end
26
20
  end
27
21
  end
@@ -1,6 +1,7 @@
1
1
  require File.expand_path('../../test_helper', __FILE__)
2
2
  require 'test_runner'
3
3
  require 'mocha/configuration'
4
+ require 'mocha/mockery'
4
5
  require 'introspection'
5
6
 
6
7
  if Mocha::Detection::MiniTest.testcase && (ENV['MOCHA_RUN_INTEGRATION_TESTS'] != 'test-unit')
@@ -31,7 +31,7 @@ class DisplayMatchingInvocationsAlongsideExpectationsTest < Mocha::TestCase
31
31
  test_result = run_as_test do
32
32
  foo = mock('foo')
33
33
  foo.expects(:bar).with(1).returns('a')
34
- foo.stubs(:bar).with(any_parameters).multiple_yields(%w[b c], %w[d e]).returns('f').raises(StandardError).throws(:tag, 'value')
34
+ foo.stubs(:bar).with(any_parameters).multiple_yields('bc', %w[d e]).returns('f').raises(StandardError).throws(:tag, 'value')
35
35
 
36
36
  foo.bar(1, 2) { |_ignored| }
37
37
  assert_raises(StandardError) { foo.bar(3, 4) { |_ignored| } }
@@ -40,9 +40,9 @@ class DisplayMatchingInvocationsAlongsideExpectationsTest < Mocha::TestCase
40
40
  assert_invocations(
41
41
  test_result,
42
42
  '- allowed any number of times, invoked 3 times: #<Mock:foo>.bar(any_parameters)',
43
- ' - #<Mock:foo>.bar(1, 2) # => "f" after yielding ("b", "c"), then ("d", "e")',
44
- ' - #<Mock:foo>.bar(3, 4) # => raised StandardError after yielding ("b", "c"), then ("d", "e")',
45
- ' - #<Mock:foo>.bar(5, 6) # => threw (:tag, "value") after yielding ("b", "c"), then ("d", "e")'
43
+ ' - #<Mock:foo>.bar(1, 2) { ... } # => "f" after yielding ("bc"), then ("d", "e")',
44
+ ' - #<Mock:foo>.bar(3, 4) { ... } # => raised StandardError after yielding ("bc"), then ("d", "e")',
45
+ ' - #<Mock:foo>.bar(5, 6) { ... } # => threw (:tag, "value") after yielding ("bc"), then ("d", "e")'
46
46
  )
47
47
  end
48
48
 
@@ -57,7 +57,7 @@ class DisplayMatchingInvocationsAlongsideExpectationsTest < Mocha::TestCase
57
57
  assert_invocations(
58
58
  test_result,
59
59
  '- allowed any number of times, invoked once: #<Mock:foo>.bar(any_parameters)',
60
- ' - #<Mock:foo>.bar(1, 2) # => nil after yielding ()'
60
+ ' - #<Mock:foo>.bar(1, 2) { ... } # => nil after yielding ()'
61
61
  )
62
62
  end
63
63
 
@@ -58,4 +58,20 @@ class FailureMessagesTest < Mocha::TestCase
58
58
  end
59
59
  assert_match Regexp.new(%("Foo")), test_result.failures[0].message
60
60
  end
61
+
62
+ def test_should_display_that_block_was_expected
63
+ test_result = run_as_test do
64
+ foo = mock
65
+ foo.expects(:bar).with_block_given
66
+ end
67
+ assert_match Regexp.new(' with block given$'), test_result.failures[0].message
68
+ end
69
+
70
+ def test_should_display_that_block_was_not_expected
71
+ test_result = run_as_test do
72
+ foo = mock
73
+ foo.expects(:bar).with_no_block_given
74
+ end
75
+ assert_match Regexp.new(' with no block given$'), test_result.failures[0].message
76
+ end
61
77
  end
@@ -0,0 +1,31 @@
1
+ require File.expand_path('../acceptance_test_helper', __FILE__)
2
+
3
+ class Issue457Test < Mocha::TestCase
4
+ include AcceptanceTest
5
+
6
+ def setup
7
+ setup_acceptance_test
8
+ end
9
+
10
+ def teardown
11
+ teardown_acceptance_test
12
+ end
13
+
14
+ def test_only_inspect_objects_when_necessary
15
+ test_result = run_as_test do
16
+ klass = Class.new do
17
+ def message
18
+ raise 'Not inspectable in this state!'
19
+ end
20
+
21
+ def inspect
22
+ message
23
+ end
24
+ end
25
+ instance = klass.new
26
+ instance.stubs(:message).returns('message')
27
+ assert_equal 'message', instance.inspect
28
+ end
29
+ assert_passed(test_result)
30
+ end
31
+ end
@@ -1,6 +1,16 @@
1
- require File.expand_path('../../test_helper', __FILE__)
1
+ require File.expand_path('../acceptance_test_helper', __FILE__)
2
2
 
3
3
  class MochaExampleTest < Mocha::TestCase
4
+ include AcceptanceTest
5
+
6
+ def setup
7
+ setup_acceptance_test
8
+ end
9
+
10
+ def teardown
11
+ teardown_acceptance_test
12
+ end
13
+
4
14
  class Rover
5
15
  def initialize(left_track, right_track, steps_per_metre, steps_per_degree)
6
16
  @left_track = left_track
@@ -0,0 +1,98 @@
1
+ require File.expand_path('../acceptance_test_helper', __FILE__)
2
+ require 'deprecation_disabler'
3
+
4
+ class MockBuiltWithFirstArgumentTypeBeingStringTest < Mocha::TestCase
5
+ include AcceptanceTest
6
+
7
+ def setup
8
+ setup_acceptance_test
9
+ end
10
+
11
+ def teardown
12
+ teardown_acceptance_test
13
+ end
14
+
15
+ def test_mock_built_with_single_symbol_argument_with_satisfied_expectation
16
+ test_result = run_as_test do
17
+ DeprecationDisabler.disable_deprecations do
18
+ m = mock(:my_method)
19
+ assert_nil m.my_method
20
+ end
21
+ expected_warning = 'Explicitly include `my_method` in Hash of expected methods vs return values, e.g. `mock(:my_method => nil)`.'
22
+ assert_equal expected_warning, Mocha::Deprecation.messages.last
23
+ end
24
+ assert_passed(test_result)
25
+ end
26
+
27
+ def test_mock_built_with_single_symbol_argument_with_unsatisfied_expectation
28
+ test_result = run_as_test do
29
+ DeprecationDisabler.disable_deprecations do
30
+ mock(:my_method)
31
+ end
32
+ end
33
+ assert_failed(test_result)
34
+ assert(test_result.failure_message_lines.any? do |line|
35
+ line[/expected exactly once, invoked never\: #<Mock\:0x[0-9a-f]+>\.my_method\(any_parameters\)/]
36
+ end)
37
+ end
38
+
39
+ def test_stub_built_with_single_symbol_argument
40
+ test_result = run_as_test do
41
+ DeprecationDisabler.disable_deprecations do
42
+ s = stub(:my_method)
43
+ assert_nil s.my_method
44
+ end
45
+ expected_warning = 'Explicitly include `my_method` in Hash of stubbed methods vs return values, e.g. `stub(:my_method => nil)`.'
46
+ assert_equal expected_warning, Mocha::Deprecation.messages.last
47
+ end
48
+ assert_passed(test_result)
49
+ end
50
+
51
+ def test_mock_built_with_first_argument_a_symbol_and_second_argument_a_hash
52
+ test_result = run_as_test do
53
+ DeprecationDisabler.disable_deprecations do
54
+ s = mock(:my_method, :another_method => 123)
55
+ assert_nil s.my_method
56
+ end
57
+ expected_warning = 'In this case the 2nd argument for `mock(:#my_method, ...)` is ignored, but in the future a Hash of expected methods vs return values will be respected.'
58
+ assert Mocha::Deprecation.messages.last(2).include?(expected_warning)
59
+ end
60
+ assert_passed(test_result)
61
+ end
62
+
63
+ def test_stub_built_with_first_argument_a_symbol_and_second_argument_a_hash
64
+ test_result = run_as_test do
65
+ DeprecationDisabler.disable_deprecations do
66
+ s = stub(:my_method, :another_method => 123)
67
+ assert_nil s.my_method
68
+ end
69
+ expected_warning = 'In this case the 2nd argument for `stub(:#my_method, ...)` is ignored, but in the future a Hash of stubbed methods vs return values will be respected.'
70
+ assert Mocha::Deprecation.messages.last(2).include?(expected_warning)
71
+ end
72
+ assert_passed(test_result)
73
+ end
74
+
75
+ def test_stub_everything_built_with_single_symbol_argument
76
+ test_result = run_as_test do
77
+ DeprecationDisabler.disable_deprecations do
78
+ s = stub_everything(:my_method)
79
+ assert_nil s.my_method
80
+ end
81
+ expected_warning = 'Explicitly include `my_method` in Hash of stubbed methods vs return values, e.g. `stub_everything(:my_method => nil)`.'
82
+ assert_equal expected_warning, Mocha::Deprecation.messages.last
83
+ end
84
+ assert_passed(test_result)
85
+ end
86
+
87
+ def test_stub_everything_built_with_first_argument_a_symbol_and_second_argument_a_hash
88
+ test_result = run_as_test do
89
+ DeprecationDisabler.disable_deprecations do
90
+ s = stub_everything(:my_method, :another_method => 123)
91
+ assert_nil s.my_method
92
+ end
93
+ expected_warning = 'In this case the 2nd argument for `stub_everything(:#my_method, ...)` is ignored, but in the future a Hash of stubbed methods vs return values will be respected.' # rubocop:disable Metrics/LineLength
94
+ assert Mocha::Deprecation.messages.last(2).include?(expected_warning)
95
+ end
96
+ assert_passed(test_result)
97
+ end
98
+ end
@@ -1,7 +1,11 @@
1
1
  require File.expand_path('../acceptance_test_helper', __FILE__)
2
+ require 'mocha/configuration'
3
+ require 'mocha/deprecation'
4
+ require 'deprecation_disabler'
2
5
 
3
6
  class MockTest < Mocha::TestCase
4
7
  include AcceptanceTest
8
+ include Mocha
5
9
 
6
10
  def setup
7
11
  setup_acceptance_test
@@ -39,9 +43,13 @@ class MockTest < Mocha::TestCase
39
43
 
40
44
  def test_should_build_symbol_named_mock_and_explicitly_add_an_expectation_which_is_satisfied
41
45
  test_result = run_as_test do
42
- foo = mock(:foo)
43
- foo.expects(:bar)
44
- foo.bar
46
+ Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => false) do
47
+ DeprecationDisabler.disable_deprecations do
48
+ foo = mock(:foo)
49
+ foo.expects(:bar)
50
+ foo.bar
51
+ end
52
+ end
45
53
  end
46
54
  assert_passed(test_result)
47
55
  end
@@ -56,8 +64,10 @@ class MockTest < Mocha::TestCase
56
64
 
57
65
  def test_should_build_symbol_named_mock_and_explicitly_add_an_expectation_which_is_not_satisfied
58
66
  test_result = run_as_test do
59
- foo = mock(:foo)
60
- foo.expects(:bar)
67
+ DeprecationDisabler.disable_deprecations do
68
+ foo = mock(:foo)
69
+ foo.expects(:bar)
70
+ end
61
71
  end
62
72
  assert_failed(test_result)
63
73
  end
@@ -98,9 +108,13 @@ class MockTest < Mocha::TestCase
98
108
 
99
109
  def test_should_build_symbol_named_mock_incorporating_two_expectations_which_are_satisifed
100
110
  test_result = run_as_test do
101
- foo = mock(:foo, :bar => 'bar', :baz => 'baz')
102
- foo.bar
103
- foo.baz
111
+ Mocha::Configuration.override(:reinstate_undocumented_behaviour_from_v1_9 => false) do
112
+ DeprecationDisabler.disable_deprecations do
113
+ foo = mock(:foo, :bar => 'bar', :baz => 'baz')
114
+ foo.bar
115
+ foo.baz
116
+ end
117
+ end
104
118
  end
105
119
  assert_passed(test_result)
106
120
  end
@@ -115,8 +129,10 @@ class MockTest < Mocha::TestCase
115
129
 
116
130
  def test_should_build_symbol_named_mock_incorporating_two_expectations_the_first_of_which_is_not_satisifed
117
131
  test_result = run_as_test do
118
- foo = mock(:foo, :bar => 'bar', :baz => 'baz')
119
- foo.baz
132
+ DeprecationDisabler.disable_deprecations do
133
+ foo = mock(:foo, :bar => 'bar', :baz => 'baz')
134
+ foo.baz
135
+ end
120
136
  end
121
137
  assert_failed(test_result)
122
138
  end
@@ -131,9 +147,45 @@ class MockTest < Mocha::TestCase
131
147
 
132
148
  def test_should_build_symbol_named_mock_incorporating_two_expectations_the_second_of_which_is_not_satisifed
133
149
  test_result = run_as_test do
134
- foo = mock(:foo, :bar => 'bar', :baz => 'baz')
135
- foo.bar
150
+ DeprecationDisabler.disable_deprecations do
151
+ foo = mock(:foo, :bar => 'bar', :baz => 'baz')
152
+ foo.bar
153
+ end
136
154
  end
137
155
  assert_failed(test_result)
138
156
  end
157
+
158
+ class Foo
159
+ class << self
160
+ attr_accessor :logger
161
+ end
162
+
163
+ def use_the_mock
164
+ self.class.logger.log('Foo was here')
165
+ end
166
+ end
167
+
168
+ # rubocop:disable Metrics/AbcSize
169
+ def test_should_display_deprecation_warning_if_mock_receives_invocations_in_another_test
170
+ use_mock_test_result = run_as_test do
171
+ Foo.logger = mock('Logger')
172
+ Foo.logger.expects(:log).with('Foo was here')
173
+ Foo.new.use_the_mock
174
+ end
175
+ assert_passed(use_mock_test_result)
176
+
177
+ reuse_mock_test_result = run_as_test do
178
+ DeprecationDisabler.disable_deprecations do
179
+ Foo.logger.expects(:log).with('Foo was here')
180
+ Foo.new.use_the_mock
181
+ end
182
+ end
183
+ assert_passed(reuse_mock_test_result)
184
+ assert message = Deprecation.messages.last
185
+ assert message.include?('#<Mock:Logger> was instantiated in one test but it is receiving invocations within another test.')
186
+ assert message.include?('This can lead to unintended interactions between tests and hence unexpected test failures.')
187
+ assert message.include?('Ensure that every test correctly cleans up any state that it introduces.')
188
+ assert message.include?('A Mocha::StubbingError will be raised in this scenario in the future.')
189
+ end
190
+ # rubocop:enable Metrics/AbcSize
139
191
  end