mcmire-mocha 0.9.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (178) hide show
  1. data/COPYING +3 -0
  2. data/MIT-LICENSE +7 -0
  3. data/README +39 -0
  4. data/RELEASE +294 -0
  5. data/Rakefile +214 -0
  6. data/examples/misc.rb +43 -0
  7. data/examples/mocha.rb +25 -0
  8. data/examples/stubba.rb +64 -0
  9. data/lib/mocha.rb +3 -0
  10. data/lib/mocha/any_instance_method.rb +59 -0
  11. data/lib/mocha/api.rb +249 -0
  12. data/lib/mocha/argument_iterator.rb +21 -0
  13. data/lib/mocha/backtrace_filter.rb +17 -0
  14. data/lib/mocha/cardinality.rb +95 -0
  15. data/lib/mocha/central.rb +27 -0
  16. data/lib/mocha/change_state_side_effect.rb +19 -0
  17. data/lib/mocha/class_method.rb +91 -0
  18. data/lib/mocha/configuration.rb +79 -0
  19. data/lib/mocha/deprecation.rb +22 -0
  20. data/lib/mocha/exception_raiser.rb +17 -0
  21. data/lib/mocha/expectation.rb +484 -0
  22. data/lib/mocha/expectation_error.rb +15 -0
  23. data/lib/mocha/expectation_list.rb +50 -0
  24. data/lib/mocha/in_state_ordering_constraint.rb +19 -0
  25. data/lib/mocha/inspect.rb +67 -0
  26. data/lib/mocha/instance_method.rb +16 -0
  27. data/lib/mocha/integration.rb +38 -0
  28. data/lib/mocha/integration/mini_test.rb +21 -0
  29. data/lib/mocha/integration/mini_test/assertion_counter.rb +23 -0
  30. data/lib/mocha/integration/mini_test/version_131_and_above.rb +50 -0
  31. data/lib/mocha/integration/test_unit.rb +40 -0
  32. data/lib/mocha/integration/test_unit/assertion_counter.rb +23 -0
  33. data/lib/mocha/integration/test_unit/gem_version_200.rb +49 -0
  34. data/lib/mocha/integration/test_unit/gem_version_201_and_above.rb +49 -0
  35. data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +48 -0
  36. data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +50 -0
  37. data/lib/mocha/invocation.rb +10 -0
  38. data/lib/mocha/is_a.rb +9 -0
  39. data/lib/mocha/logger.rb +15 -0
  40. data/lib/mocha/metaclass.rb +13 -0
  41. data/lib/mocha/method_matcher.rb +21 -0
  42. data/lib/mocha/mock.rb +200 -0
  43. data/lib/mocha/mockery.rb +189 -0
  44. data/lib/mocha/module_method.rb +16 -0
  45. data/lib/mocha/multiple_yields.rb +20 -0
  46. data/lib/mocha/names.rb +53 -0
  47. data/lib/mocha/no_yields.rb +11 -0
  48. data/lib/mocha/object.rb +187 -0
  49. data/lib/mocha/parameter_matchers.rb +27 -0
  50. data/lib/mocha/parameter_matchers/all_of.rb +42 -0
  51. data/lib/mocha/parameter_matchers/any_of.rb +47 -0
  52. data/lib/mocha/parameter_matchers/any_parameters.rb +40 -0
  53. data/lib/mocha/parameter_matchers/anything.rb +33 -0
  54. data/lib/mocha/parameter_matchers/base.rb +15 -0
  55. data/lib/mocha/parameter_matchers/equals.rb +42 -0
  56. data/lib/mocha/parameter_matchers/has_entries.rb +45 -0
  57. data/lib/mocha/parameter_matchers/has_entry.rb +57 -0
  58. data/lib/mocha/parameter_matchers/has_key.rb +43 -0
  59. data/lib/mocha/parameter_matchers/has_value.rb +43 -0
  60. data/lib/mocha/parameter_matchers/includes.rb +41 -0
  61. data/lib/mocha/parameter_matchers/instance_of.rb +42 -0
  62. data/lib/mocha/parameter_matchers/is_a.rb +42 -0
  63. data/lib/mocha/parameter_matchers/kind_of.rb +42 -0
  64. data/lib/mocha/parameter_matchers/not.rb +42 -0
  65. data/lib/mocha/parameter_matchers/object.rb +15 -0
  66. data/lib/mocha/parameter_matchers/optionally.rb +55 -0
  67. data/lib/mocha/parameter_matchers/regexp_matches.rb +44 -0
  68. data/lib/mocha/parameter_matchers/responds_with.rb +43 -0
  69. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +43 -0
  70. data/lib/mocha/parameters_matcher.rb +37 -0
  71. data/lib/mocha/pretty_parameters.rb +28 -0
  72. data/lib/mocha/return_values.rb +31 -0
  73. data/lib/mocha/sequence.rb +42 -0
  74. data/lib/mocha/single_return_value.rb +17 -0
  75. data/lib/mocha/single_yield.rb +18 -0
  76. data/lib/mocha/standalone.rb +1 -0
  77. data/lib/mocha/state_machine.rb +91 -0
  78. data/lib/mocha/stubbing_error.rb +16 -0
  79. data/lib/mocha/unexpected_invocation.rb +18 -0
  80. data/lib/mocha/yield_parameters.rb +31 -0
  81. data/lib/mocha_standalone.rb +2 -0
  82. data/lib/stubba.rb +4 -0
  83. data/test/acceptance/acceptance_test_helper.rb +38 -0
  84. data/test/acceptance/api_test.rb +139 -0
  85. data/test/acceptance/bug_18914_test.rb +43 -0
  86. data/test/acceptance/bug_21465_test.rb +34 -0
  87. data/test/acceptance/bug_21563_test.rb +25 -0
  88. data/test/acceptance/expected_invocation_count_test.rb +196 -0
  89. data/test/acceptance/failure_messages_test.rb +64 -0
  90. data/test/acceptance/minitest_test.rb +153 -0
  91. data/test/acceptance/mocha_example_test.rb +98 -0
  92. data/test/acceptance/mocha_test_result_test.rb +84 -0
  93. data/test/acceptance/mock_test.rb +100 -0
  94. data/test/acceptance/mock_with_initializer_block_test.rb +51 -0
  95. data/test/acceptance/mocked_methods_dispatch_test.rb +78 -0
  96. data/test/acceptance/optional_parameters_test.rb +70 -0
  97. data/test/acceptance/parameter_matcher_test.rb +209 -0
  98. data/test/acceptance/partial_mocks_test.rb +47 -0
  99. data/test/acceptance/return_value_test.rb +52 -0
  100. data/test/acceptance/sequence_test.rb +186 -0
  101. data/test/acceptance/spy_test.rb +134 -0
  102. data/test/acceptance/states_test.rb +70 -0
  103. data/test/acceptance/stub_any_instance_method_test.rb +195 -0
  104. data/test/acceptance/stub_class_method_test.rb +203 -0
  105. data/test/acceptance/stub_everything_test.rb +56 -0
  106. data/test/acceptance/stub_instance_method_test.rb +203 -0
  107. data/test/acceptance/stub_module_method_test.rb +163 -0
  108. data/test/acceptance/stub_test.rb +52 -0
  109. data/test/acceptance/stubba_example_test.rb +102 -0
  110. data/test/acceptance/stubba_test.rb +15 -0
  111. data/test/acceptance/stubba_test_result_test.rb +66 -0
  112. data/test/acceptance/stubbing_error_backtrace_test.rb +64 -0
  113. data/test/acceptance/stubbing_method_unnecessarily_test.rb +65 -0
  114. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +130 -0
  115. data/test/acceptance/stubbing_non_existent_class_method_test.rb +157 -0
  116. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +147 -0
  117. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +130 -0
  118. data/test/acceptance/stubbing_non_public_class_method_test.rb +163 -0
  119. data/test/acceptance/stubbing_non_public_instance_method_test.rb +143 -0
  120. data/test/acceptance/stubbing_on_non_mock_object_test.rb +64 -0
  121. data/test/deprecation_disabler.rb +15 -0
  122. data/test/execution_point.rb +36 -0
  123. data/test/matcher_helpers.rb +5 -0
  124. data/test/method_definer.rb +24 -0
  125. data/test/simple_counter.rb +13 -0
  126. data/test/test_helper.rb +16 -0
  127. data/test/test_runner.rb +33 -0
  128. data/test/unit/any_instance_method_test.rb +126 -0
  129. data/test/unit/array_inspect_test.rb +16 -0
  130. data/test/unit/assert_received_test.rb +145 -0
  131. data/test/unit/backtrace_filter_test.rb +19 -0
  132. data/test/unit/cardinality_test.rb +56 -0
  133. data/test/unit/central_test.rb +65 -0
  134. data/test/unit/change_state_side_effect_test.rb +41 -0
  135. data/test/unit/class_method_test.rb +237 -0
  136. data/test/unit/configuration_test.rb +38 -0
  137. data/test/unit/date_time_inspect_test.rb +21 -0
  138. data/test/unit/exception_raiser_test.rb +42 -0
  139. data/test/unit/expectation_list_test.rb +57 -0
  140. data/test/unit/expectation_test.rb +526 -0
  141. data/test/unit/hash_inspect_test.rb +16 -0
  142. data/test/unit/have_received_test.rb +192 -0
  143. data/test/unit/in_state_ordering_constraint_test.rb +43 -0
  144. data/test/unit/invocation_test.rb +17 -0
  145. data/test/unit/metaclass_test.rb +22 -0
  146. data/test/unit/method_matcher_test.rb +23 -0
  147. data/test/unit/mock_test.rb +329 -0
  148. data/test/unit/mockery_test.rb +163 -0
  149. data/test/unit/multiple_yields_test.rb +18 -0
  150. data/test/unit/no_yields_test.rb +18 -0
  151. data/test/unit/object_inspect_test.rb +37 -0
  152. data/test/unit/object_test.rb +82 -0
  153. data/test/unit/parameter_matchers/all_of_test.rb +26 -0
  154. data/test/unit/parameter_matchers/any_of_test.rb +26 -0
  155. data/test/unit/parameter_matchers/anything_test.rb +21 -0
  156. data/test/unit/parameter_matchers/equals_test.rb +25 -0
  157. data/test/unit/parameter_matchers/has_entries_test.rb +51 -0
  158. data/test/unit/parameter_matchers/has_entry_test.rb +82 -0
  159. data/test/unit/parameter_matchers/has_key_test.rb +55 -0
  160. data/test/unit/parameter_matchers/has_value_test.rb +57 -0
  161. data/test/unit/parameter_matchers/includes_test.rb +44 -0
  162. data/test/unit/parameter_matchers/instance_of_test.rb +25 -0
  163. data/test/unit/parameter_matchers/is_a_test.rb +25 -0
  164. data/test/unit/parameter_matchers/kind_of_test.rb +25 -0
  165. data/test/unit/parameter_matchers/not_test.rb +26 -0
  166. data/test/unit/parameter_matchers/regexp_matches_test.rb +46 -0
  167. data/test/unit/parameter_matchers/responds_with_test.rb +25 -0
  168. data/test/unit/parameter_matchers/stub_matcher.rb +27 -0
  169. data/test/unit/parameter_matchers/yaml_equivalent_test.rb +25 -0
  170. data/test/unit/parameters_matcher_test.rb +121 -0
  171. data/test/unit/return_values_test.rb +63 -0
  172. data/test/unit/sequence_test.rb +104 -0
  173. data/test/unit/single_return_value_test.rb +14 -0
  174. data/test/unit/single_yield_test.rb +18 -0
  175. data/test/unit/state_machine_test.rb +98 -0
  176. data/test/unit/string_inspect_test.rb +11 -0
  177. data/test/unit/yield_parameters_test.rb +93 -0
  178. metadata +245 -0
@@ -0,0 +1,42 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+
3
+ require 'mocha/exception_raiser'
4
+ require 'timeout'
5
+
6
+ class ExceptionRaiserTest < Test::Unit::TestCase
7
+
8
+ include Mocha
9
+
10
+ def test_should_raise_exception_with_specified_class_and_default_message
11
+ exception_class = Class.new(StandardError)
12
+ raiser = ExceptionRaiser.new(exception_class, nil)
13
+ exception = assert_raises(exception_class) { raiser.evaluate }
14
+ assert_equal exception_class.to_s, exception.message
15
+ end
16
+
17
+ def test_should_raise_exception_with_specified_class_and_message
18
+ exception_class = Class.new(StandardError)
19
+ raiser = ExceptionRaiser.new(exception_class, 'message')
20
+ exception = assert_raises(exception_class) { raiser.evaluate }
21
+ assert_equal 'message', exception.message
22
+ end
23
+
24
+ def test_should_raise_exception_instance
25
+ exception_class = Class.new(StandardError)
26
+ raiser = ExceptionRaiser.new(exception_class.new('message'), nil)
27
+ exception = assert_raises(exception_class) { raiser.evaluate }
28
+ assert_equal 'message', exception.message
29
+ end
30
+
31
+ def test_should_raise_interrupt_exception_with_default_message_so_it_works_in_ruby_1_8_6
32
+ raiser = ExceptionRaiser.new(Interrupt, nil)
33
+ assert_raises(Interrupt) { raiser.evaluate }
34
+ end
35
+
36
+ def test_should_raise_subclass_of_interrupt_exception_with_default_message_so_it_works_in_ruby_1_8_6
37
+ exception_class = Class.new(Interrupt)
38
+ raiser = ExceptionRaiser.new(exception_class, nil)
39
+ assert_raises(exception_class) { raiser.evaluate }
40
+ end
41
+
42
+ end
@@ -0,0 +1,57 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha/expectation_list'
3
+ require 'mocha/expectation'
4
+ require 'set'
5
+ require 'method_definer'
6
+
7
+ class ExpectationListTest < Test::Unit::TestCase
8
+
9
+ include Mocha
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
+
17
+ def test_should_find_matching_expectation
18
+ expectation_list = ExpectationList.new
19
+ expectation1 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
20
+ expectation2 = Expectation.new(nil, :my_method).with(:argument3, :argument4)
21
+ expectation_list.add(expectation1)
22
+ expectation_list.add(expectation2)
23
+ assert_same expectation1, expectation_list.match(:my_method, :argument1, :argument2)
24
+ end
25
+
26
+ def test_should_find_most_recent_matching_expectation
27
+ expectation_list = ExpectationList.new
28
+ expectation1 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
29
+ expectation2 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
30
+ expectation_list.add(expectation1)
31
+ expectation_list.add(expectation2)
32
+ assert_same expectation2, expectation_list.match(:my_method, :argument1, :argument2)
33
+ end
34
+
35
+ def test_should_find_matching_expectation_allowing_invocation
36
+ expectation_list = ExpectationList.new
37
+ expectation1 = Expectation.new(nil, :my_method).with(:argument1, :argument2)
38
+ expectation2 = Expectation.new(nil, :my_method).with(:argument3, :argument4)
39
+ expectation1.define_instance_method(:invocations_allowed?) { true }
40
+ expectation2.define_instance_method(:invocations_allowed?) { true }
41
+ expectation_list.add(expectation1)
42
+ expectation_list.add(expectation2)
43
+ assert_same expectation1, expectation_list.match_allowing_invocation(:my_method, :argument1, :argument2)
44
+ end
45
+
46
+ def test_should_find_most_recent_matching_expectation_allowing_invocation
47
+ expectation_list = ExpectationList.new
48
+ expectation1 = Expectation.new(nil, :my_method)
49
+ expectation2 = Expectation.new(nil, :my_method)
50
+ expectation1.define_instance_method(:invocations_allowed?) { true }
51
+ expectation2.define_instance_method(:invocations_allowed?) { false }
52
+ expectation_list.add(expectation1)
53
+ expectation_list.add(expectation2)
54
+ assert_same expectation1, expectation_list.match_allowing_invocation(:my_method)
55
+ end
56
+
57
+ end
@@ -0,0 +1,526 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'method_definer'
3
+ require 'mocha/expectation'
4
+ require 'mocha/sequence'
5
+ require 'mocha/mockery'
6
+ require 'execution_point'
7
+ require 'simple_counter'
8
+
9
+ class ExpectationTest < Test::Unit::TestCase
10
+
11
+ include Mocha
12
+
13
+ class FakeMockery
14
+ attr_reader :invocations
15
+
16
+ def initialize
17
+ @invocations = []
18
+ end
19
+
20
+ def invocation(mock, method_name, args)
21
+ @invocations << { :mock => mock, :method_name => method_name, :args => args }
22
+ end
23
+ end
24
+
25
+ def setup
26
+ Mockery.instance_variable_set('@instance', FakeMockery.new)
27
+ end
28
+
29
+ def teardown
30
+ Mockery.reset_instance
31
+ end
32
+
33
+ def new_expectation
34
+ Expectation.new(nil, :expected_method)
35
+ end
36
+
37
+ def test_should_match_calls_to_same_method_with_any_parameters
38
+ assert new_expectation.match?(:expected_method, 1, 2, 3)
39
+ end
40
+
41
+ def test_should_match_calls_to_same_method_with_exactly_zero_parameters
42
+ expectation = new_expectation.with()
43
+ assert expectation.match?(:expected_method)
44
+ end
45
+
46
+ def test_should_not_match_calls_to_same_method_with_more_than_zero_parameters
47
+ expectation = new_expectation.with()
48
+ assert !expectation.match?(:expected_method, 1, 2, 3)
49
+ end
50
+
51
+ def test_should_match_calls_to_same_method_with_expected_parameter_values
52
+ expectation = new_expectation.with(1, 2, 3)
53
+ assert expectation.match?(:expected_method, 1, 2, 3)
54
+ end
55
+
56
+ def test_should_match_calls_to_same_method_with_parameters_constrained_as_expected
57
+ expectation = new_expectation.with() {|x, y, z| x + y == z}
58
+ assert expectation.match?(:expected_method, 1, 2, 3)
59
+ end
60
+
61
+ def test_should_not_match_calls_to_different_method_with_parameters_constrained_as_expected
62
+ expectation = new_expectation.with() {|x, y, z| x + y == z}
63
+ assert !expectation.match?(:different_method, 1, 2, 3)
64
+ end
65
+
66
+ def test_should_not_match_calls_to_different_methods_with_no_parameters
67
+ assert !new_expectation.match?(:unexpected_method)
68
+ end
69
+
70
+ def test_should_not_match_calls_to_same_method_with_too_few_parameters
71
+ expectation = new_expectation.with(1, 2, 3)
72
+ assert !expectation.match?(:unexpected_method, 1, 2)
73
+ end
74
+
75
+ def test_should_not_match_calls_to_same_method_with_too_many_parameters
76
+ expectation = new_expectation.with(1, 2)
77
+ assert !expectation.match?(:unexpected_method, 1, 2, 3)
78
+ end
79
+
80
+ def test_should_not_match_calls_to_same_method_with_unexpected_parameter_values
81
+ expectation = new_expectation.with(1, 2, 3)
82
+ assert !expectation.match?(:unexpected_method, 1, 0, 3)
83
+ end
84
+
85
+ def test_should_not_match_calls_to_same_method_with_parameters_not_constrained_as_expected
86
+ expectation = new_expectation.with() {|x, y, z| x + y == z}
87
+ assert !expectation.match?(:expected_method, 1, 0, 3)
88
+ end
89
+
90
+ def test_should_allow_invocations_until_expected_invocation_count_is_one_and_actual_invocation_count_would_be_two
91
+ expectation = new_expectation.times(1)
92
+ assert expectation.invocations_allowed?
93
+ expectation.invoke([])
94
+ assert !expectation.invocations_allowed?
95
+ end
96
+
97
+ def test_should_allow_invocations_until_expected_invocation_count_is_two_and_actual_invocation_count_would_be_three
98
+ expectation = new_expectation.times(2)
99
+ assert expectation.invocations_allowed?
100
+ expectation.invoke([])
101
+ assert expectation.invocations_allowed?
102
+ expectation.invoke([])
103
+ assert !expectation.invocations_allowed?
104
+ end
105
+
106
+ def test_should_allow_invocations_until_expected_invocation_count_is_a_range_from_two_to_three_and_actual_invocation_count_would_be_four
107
+ expectation = new_expectation.times(2..3)
108
+ assert expectation.invocations_allowed?
109
+ expectation.invoke([])
110
+ assert expectation.invocations_allowed?
111
+ expectation.invoke([])
112
+ assert expectation.invocations_allowed?
113
+ expectation.invoke([])
114
+ assert !expectation.invocations_allowed?
115
+ end
116
+
117
+ def test_should_store_provided_backtrace
118
+ backtrace = Object.new
119
+ expectation = Expectation.new(nil, :expected_method, backtrace)
120
+ assert_equal backtrace, expectation.backtrace
121
+ end
122
+
123
+ def test_should_default_backtrace_to_caller
124
+ execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
125
+ assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
126
+ end
127
+
128
+ def test_should_not_yield
129
+ yielded = false
130
+ new_expectation.invoke([]) { yielded = true }
131
+ assert_equal false, yielded
132
+ end
133
+
134
+ def test_should_yield_no_parameters
135
+ expectation = new_expectation().yields()
136
+ yielded_parameters = nil
137
+ expectation.invoke([]) { |*parameters| yielded_parameters = parameters }
138
+ assert_equal Array.new, yielded_parameters
139
+ end
140
+
141
+ def test_should_yield_with_specified_parameters
142
+ expectation = new_expectation().yields(1, 2, 3)
143
+ yielded_parameters = nil
144
+ expectation.invoke([]) { |*parameters| yielded_parameters = parameters }
145
+ assert_equal [1, 2, 3], yielded_parameters
146
+ end
147
+
148
+ def test_should_yield_different_parameters_on_consecutive_invocations
149
+ expectation = new_expectation().yields(1, 2, 3).yields(4, 5)
150
+ yielded_parameters = []
151
+ expectation.invoke([]) { |*parameters| yielded_parameters << parameters }
152
+ expectation.invoke([]) { |*parameters| yielded_parameters << parameters }
153
+ assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
154
+ end
155
+
156
+ def test_should_yield_multiple_times_for_single_invocation
157
+ expectation = new_expectation().multiple_yields([1, 2, 3], [4, 5])
158
+ yielded_parameters = []
159
+ expectation.invoke([]) { |*parameters| yielded_parameters << parameters }
160
+ assert_equal [[1, 2, 3], [4, 5]], yielded_parameters
161
+ end
162
+
163
+ def test_should_yield_multiple_times_for_first_invocation_and_once_for_second_invocation
164
+ expectation = new_expectation().multiple_yields([1, 2, 3], [4, 5]).then.yields(6, 7)
165
+ yielded_parameters = []
166
+ expectation.invoke([]) { |*parameters| yielded_parameters << parameters }
167
+ expectation.invoke([]) { |*parameters| yielded_parameters << parameters }
168
+ assert_equal [[1, 2, 3], [4, 5], [6, 7]], yielded_parameters
169
+ end
170
+
171
+ def test_should_return_specified_value
172
+ expectation = new_expectation.returns(99)
173
+ assert_equal 99, expectation.invoke([])
174
+ end
175
+
176
+ def test_should_return_same_specified_value_multiple_times
177
+ expectation = new_expectation.returns(99)
178
+ assert_equal 99, expectation.invoke([])
179
+ assert_equal 99, expectation.invoke([])
180
+ end
181
+
182
+ def test_should_return_specified_values_on_consecutive_calls
183
+ expectation = new_expectation.returns(99, 100, 101)
184
+ assert_equal 99, expectation.invoke([])
185
+ assert_equal 100, expectation.invoke([])
186
+ assert_equal 101, expectation.invoke([])
187
+ end
188
+
189
+ def test_should_return_specified_values_on_consecutive_calls_even_if_values_are_modified
190
+ values = [99, 100, 101]
191
+ expectation = new_expectation.returns(*values)
192
+ values.shift
193
+ assert_equal 99, expectation.invoke([])
194
+ assert_equal 100, expectation.invoke([])
195
+ assert_equal 101, expectation.invoke([])
196
+ end
197
+
198
+ def test_should_return_nil_by_default
199
+ assert_nil new_expectation.invoke([])
200
+ end
201
+
202
+ def test_should_return_nil_if_no_value_specified
203
+ expectation = new_expectation.returns()
204
+ assert_nil expectation.invoke([])
205
+ end
206
+
207
+ def test_should_raise_runtime_exception
208
+ expectation = new_expectation.raises
209
+ assert_raise(RuntimeError) { expectation.invoke([]) }
210
+ end
211
+
212
+ def test_should_raise_custom_exception
213
+ exception = Class.new(Exception)
214
+ expectation = new_expectation.raises(exception)
215
+ assert_raise(exception) { expectation.invoke([]) }
216
+ end
217
+
218
+ def test_should_raise_same_instance_of_custom_exception
219
+ exception_klass = Class.new(StandardError)
220
+ expected_exception = exception_klass.new
221
+ expectation = new_expectation.raises(expected_exception)
222
+ actual_exception = assert_raise(exception_klass) { expectation.invoke([]) }
223
+ assert_same expected_exception, actual_exception
224
+ end
225
+
226
+ def test_should_use_the_default_exception_message
227
+ expectation = new_expectation.raises(Exception)
228
+ exception = assert_raise(Exception) { expectation.invoke([]) }
229
+ assert_equal Exception.new.message, exception.message
230
+ end
231
+
232
+ def test_should_raise_custom_exception_with_message
233
+ exception_msg = "exception message"
234
+ expectation = new_expectation.raises(Exception, exception_msg)
235
+ exception = assert_raise(Exception) { expectation.invoke([]) }
236
+ assert_equal exception_msg, exception.message
237
+ end
238
+
239
+ def test_should_return_values_then_raise_exception
240
+ expectation = new_expectation.returns(1, 2).then.raises()
241
+ assert_equal 1, expectation.invoke([])
242
+ assert_equal 2, expectation.invoke([])
243
+ assert_raise(RuntimeError) { expectation.invoke([]) }
244
+ end
245
+
246
+ def test_should_raise_exception_then_return_values
247
+ expectation = new_expectation.raises().then.returns(1, 2)
248
+ assert_raise(RuntimeError) { expectation.invoke([]) }
249
+ assert_equal 1, expectation.invoke([])
250
+ assert_equal 2, expectation.invoke([])
251
+ end
252
+
253
+ def test_should_verify_successfully_if_expected_call_was_made
254
+ expectation = new_expectation
255
+ expectation.invoke([])
256
+ assert expectation.verified?
257
+ end
258
+
259
+ def test_should_not_verify_successfully_if_call_expected_once_but_invoked_twice
260
+ expectation = new_expectation.once
261
+ expectation.invoke([])
262
+ expectation.invoke([])
263
+ assert !expectation.verified?
264
+ end
265
+
266
+ def test_should_not_verify_successfully_if_call_expected_once_but_not_invoked
267
+ expectation = new_expectation.once
268
+ assert !expectation.verified?
269
+ end
270
+
271
+ def test_should_verify_successfully_if_call_expected_once_and_invoked_once
272
+ expectation = new_expectation.once
273
+ expectation.invoke([])
274
+ assert expectation.verified?
275
+ end
276
+
277
+ def test_should_not_verify_successfully_if_call_expected_twice_and_invoked_three_times
278
+ expectation = new_expectation.twice
279
+ expectation.invoke([])
280
+ expectation.invoke([])
281
+ expectation.invoke([])
282
+ assert !expectation.verified?
283
+ end
284
+
285
+ def test_should_not_verify_successfully_if_call_expected_twice_but_invoked_once
286
+ expectation = new_expectation.twice
287
+ expectation.invoke([])
288
+ assert !expectation.verified?
289
+ end
290
+
291
+ def test_should_verify_successfully_if_call_expected_twice_and_invoked_twice
292
+ expectation = new_expectation.twice
293
+ expectation.invoke([])
294
+ expectation.invoke([])
295
+ assert expectation.verified?
296
+ end
297
+
298
+ def test_should_verify_successfully_if_expected_call_was_made_at_least_once
299
+ expectation = new_expectation.at_least_once
300
+ 3.times {expectation.invoke([])}
301
+ assert expectation.verified?
302
+ end
303
+
304
+ def test_should_not_verify_successfully_if_expected_call_was_not_made_at_least_once
305
+ expectation = new_expectation.with(1, 2, 3).at_least_once
306
+ assert !expectation.verified?
307
+ assert_match(/expected at least once, not yet invoked/i, expectation.mocha_inspect)
308
+ end
309
+
310
+ def test_should_verify_successfully_if_expected_call_was_made_expected_number_of_times
311
+ expectation = new_expectation.times(2)
312
+ 2.times {expectation.invoke([])}
313
+ assert expectation.verified?
314
+ end
315
+
316
+ def test_should_not_verify_successfully_if_expected_call_was_made_too_few_times
317
+ expectation = new_expectation.times(2)
318
+ 1.times {expectation.invoke([])}
319
+ assert !expectation.verified?
320
+ assert_match(/expected exactly twice, already invoked once/i, expectation.mocha_inspect)
321
+ end
322
+
323
+ def test_should_not_verify_successfully_if_expected_call_was_made_too_many_times
324
+ expectation = new_expectation.times(2)
325
+ 3.times {expectation.invoke([])}
326
+ assert !expectation.verified?
327
+ end
328
+
329
+ def test_should_increment_assertion_counter_for_expectation_because_it_does_need_verifyng
330
+ expectation = new_expectation
331
+ expectation.invoke([])
332
+ assertion_counter = SimpleCounter.new
333
+ expectation.verified?(assertion_counter)
334
+ assert_equal 1, assertion_counter.count
335
+ end
336
+
337
+ def test_should_not_increment_assertion_counter_for_stub_because_it_does_not_need_verifying
338
+ stub = Expectation.new(nil, :expected_method).at_least(0)
339
+ assertion_counter = SimpleCounter.new
340
+ stub.verified?(assertion_counter)
341
+ assert_equal 0, assertion_counter.count
342
+ end
343
+
344
+ def test_should_store_backtrace_from_point_where_expectation_was_created
345
+ execution_point = ExecutionPoint.current; expectation = Expectation.new(nil, :expected_method)
346
+ assert_equal execution_point, ExecutionPoint.new(expectation.backtrace)
347
+ end
348
+
349
+ class FakeMock
350
+
351
+ def initialize(name)
352
+ @name = name
353
+ end
354
+
355
+ def mocha_inspect
356
+ @name
357
+ end
358
+
359
+ end
360
+
361
+ 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
362
+ mock = FakeMock.new('mock')
363
+ sequence_one = Sequence.new('one')
364
+ sequence_two = Sequence.new('two')
365
+ expectation = Expectation.new(mock, :expected_method).with(1, 2, {'a' => true}, {:b => false}, [1, 2, 3]).in_sequence(sequence_one, sequence_two)
366
+ assert !expectation.verified?
367
+ assert_match "mock.expected_method(1, 2, {'a' => true}, {:b => false}, [1, 2, 3]); in sequence 'one'; in sequence 'two'", expectation.mocha_inspect
368
+ end
369
+
370
+ class FakeConstraint
371
+
372
+ def initialize(allows_invocation_now)
373
+ @allows_invocation_now = allows_invocation_now
374
+ end
375
+
376
+ def allows_invocation_now?
377
+ @allows_invocation_now
378
+ end
379
+
380
+ end
381
+
382
+ def test_should_be_in_correct_order_if_all_ordering_constraints_allow_invocation_now
383
+ constraint_one = FakeConstraint.new(allows_invocation_now = true)
384
+ constraint_two = FakeConstraint.new(allows_invocation_now = true)
385
+ expectation = Expectation.new(nil, :method_one)
386
+ expectation.add_ordering_constraint(constraint_one)
387
+ expectation.add_ordering_constraint(constraint_two)
388
+ assert expectation.in_correct_order?
389
+ end
390
+
391
+ def test_should_not_be_in_correct_order_if_one_ordering_constraint_does_not_allow_invocation_now
392
+ constraint_one = FakeConstraint.new(allows_invocation_now = true)
393
+ constraint_two = FakeConstraint.new(allows_invocation_now = false)
394
+ expectation = Expectation.new(nil, :method_one)
395
+ expectation.add_ordering_constraint(constraint_one)
396
+ expectation.add_ordering_constraint(constraint_two)
397
+ assert !expectation.in_correct_order?
398
+ end
399
+
400
+ def test_should_match_if_all_ordering_constraints_allow_invocation_now
401
+ constraint_one = FakeConstraint.new(allows_invocation_now = true)
402
+ constraint_two = FakeConstraint.new(allows_invocation_now = true)
403
+ expectation = Expectation.new(nil, :method_one)
404
+ expectation.add_ordering_constraint(constraint_one)
405
+ expectation.add_ordering_constraint(constraint_two)
406
+ assert expectation.match?(:method_one)
407
+ end
408
+
409
+ def test_should_not_match_if_one_ordering_constraints_does_not_allow_invocation_now
410
+ constraint_one = FakeConstraint.new(allows_invocation_now = true)
411
+ constraint_two = FakeConstraint.new(allows_invocation_now = false)
412
+ expectation = Expectation.new(nil, :method_one)
413
+ expectation.add_ordering_constraint(constraint_one)
414
+ expectation.add_ordering_constraint(constraint_two)
415
+ assert !expectation.match?(:method_one)
416
+ end
417
+
418
+ def test_should_not_be_satisfied_when_required_invocation_has_not_been_made
419
+ expectation = Expectation.new(nil, :method_one).times(1)
420
+ assert !expectation.satisfied?
421
+ end
422
+
423
+ def test_should_be_satisfied_when_required_invocation_has_been_made
424
+ expectation = Expectation.new(nil, :method_one).times(1)
425
+ expectation.invoke([])
426
+ assert expectation.satisfied?
427
+ end
428
+
429
+ def test_should_not_be_satisfied_when_minimum_number_of_invocations_has_not_been_made
430
+ expectation = Expectation.new(nil, :method_one).at_least(2)
431
+ expectation.invoke([])
432
+ assert !expectation.satisfied?
433
+ end
434
+
435
+ def test_should_be_satisfied_when_minimum_number_of_invocations_has_been_made
436
+ expectation = Expectation.new(nil, :method_one).at_least(2)
437
+ 2.times { expectation.invoke([]) }
438
+ assert expectation.satisfied?
439
+ end
440
+
441
+ class FakeSequence
442
+
443
+ attr_reader :expectations
444
+
445
+ def initialize
446
+ @expectations = []
447
+ end
448
+
449
+ def constrain_as_next_in_sequence(expectation)
450
+ @expectations << expectation
451
+ end
452
+
453
+ end
454
+
455
+ def test_should_tell_sequences_to_constrain_expectation_as_next_in_sequence
456
+ sequence_one = FakeSequence.new
457
+ sequence_two = FakeSequence.new
458
+ expectation = Expectation.new(nil, :method_one)
459
+ assert_equal expectation, expectation.in_sequence(sequence_one, sequence_two)
460
+ assert_equal [expectation], sequence_one.expectations
461
+ assert_equal [expectation], sequence_two.expectations
462
+ end
463
+
464
+ class FakeState
465
+
466
+ def initialize
467
+ @active = false
468
+ end
469
+
470
+ def activate
471
+ @active = true
472
+ end
473
+
474
+ def active?
475
+ @active
476
+ end
477
+
478
+ end
479
+
480
+ def test_should_change_state_when_expectation_is_invoked
481
+ state = FakeState.new
482
+ expectation = Expectation.new(nil, :method_one)
483
+
484
+ expectation.then(state)
485
+
486
+ expectation.invoke([])
487
+ assert state.active?
488
+ end
489
+
490
+ def test_should_match_when_state_is_active
491
+ state = FakeState.new
492
+ expectation = Expectation.new(nil, :method_one)
493
+
494
+ expectation.when(state)
495
+ assert !expectation.match?(:method_one)
496
+
497
+ state.activate
498
+ assert expectation.match?(:method_one)
499
+ end
500
+
501
+ def test_should_record_invocation
502
+ Mockery.instance.invocations.clear
503
+
504
+ mock = 'a mock'
505
+ method = :call_me
506
+ args = [1, 2, 3]
507
+ expectation = Expectation.new(mock, method)
508
+
509
+ expectation.invoke(args)
510
+ assert_equal 1, Mockery.instance.invocations.size
511
+ invocation = Mockery.instance.invocations.first
512
+ assert_equal method, invocation[:method_name], "Got: #{invocation.inspect}"
513
+ assert_equal args, invocation[:args], "Got: #{invocation.inspect}"
514
+ assert_equal mock, invocation[:mock], "Got: #{invocation.inspect}"
515
+ end
516
+
517
+ def test_should_expose_invocation_count
518
+ expectation = Expectation.new(nil, :a_method)
519
+ assert_equal 0, expectation.invocation_count
520
+ expectation.invoke(1)
521
+ assert_equal 1, expectation.invocation_count
522
+ expectation.invocation_count = 3
523
+ assert_equal 3, expectation.invocation_count
524
+ end
525
+
526
+ end