mocha 0.5.6 → 3.1.0
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.
- checksums.yaml +7 -0
- data/.gemtest +0 -0
- data/.github/FUNDING.yml +1 -0
- data/.rubocop.yml +92 -0
- data/.rubocop_todo.yml +39 -0
- data/.yardopts +25 -0
- data/CONTRIBUTING.md +7 -0
- data/COPYING.md +3 -0
- data/Gemfile +17 -0
- data/{MIT-LICENSE → MIT-LICENSE.md} +2 -2
- data/README.md +361 -0
- data/RELEASE.md +1247 -0
- data/Rakefile +165 -123
- data/gemfiles/Gemfile.minitest.latest +8 -0
- data/gemfiles/Gemfile.rubocop +9 -0
- data/gemfiles/Gemfile.test-unit.latest +8 -0
- data/lib/mocha/any_instance_method.rb +12 -26
- data/lib/mocha/any_instance_receiver.rb +20 -0
- data/lib/mocha/api.rb +213 -0
- data/lib/mocha/argument_iterator.rb +17 -0
- data/lib/mocha/backtrace_filter.rb +25 -0
- data/lib/mocha/block_matchers.rb +33 -0
- data/lib/mocha/cardinality.rb +110 -0
- data/lib/mocha/central.rb +33 -22
- data/lib/mocha/change_state_side_effect.rb +17 -0
- data/lib/mocha/class_methods.rb +67 -0
- data/lib/mocha/configuration.rb +338 -0
- data/lib/mocha/default_name.rb +15 -0
- data/lib/mocha/default_receiver.rb +13 -0
- data/lib/mocha/deprecation.rb +6 -15
- data/lib/mocha/detection/minitest.rb +25 -0
- data/lib/mocha/detection/test_unit.rb +30 -0
- data/lib/mocha/error_with_filtered_backtrace.rb +15 -0
- data/lib/mocha/exception_raiser.rb +11 -10
- data/lib/mocha/expectation.rb +562 -171
- data/lib/mocha/expectation_error.rb +9 -14
- data/lib/mocha/expectation_error_factory.rb +37 -0
- data/lib/mocha/expectation_list.rb +30 -14
- data/lib/mocha/hooks.rb +55 -0
- data/lib/mocha/ignoring_warning.rb +20 -0
- data/lib/mocha/impersonating_any_instance_name.rb +13 -0
- data/lib/mocha/impersonating_name.rb +13 -0
- data/lib/mocha/in_state_ordering_constraint.rb +17 -0
- data/lib/mocha/inspect.rb +54 -30
- data/lib/mocha/instance_method.rb +17 -4
- data/lib/mocha/integration/assertion_counter.rb +15 -0
- data/lib/mocha/integration/minitest/adapter.rb +71 -0
- data/lib/mocha/integration/minitest.rb +29 -0
- data/lib/mocha/integration/monkey_patcher.rb +26 -0
- data/lib/mocha/integration/test_unit/adapter.rb +61 -0
- data/lib/mocha/integration/test_unit.rb +29 -0
- data/lib/mocha/integration.rb +5 -0
- data/lib/mocha/invocation.rb +76 -0
- data/lib/mocha/logger.rb +26 -0
- data/lib/mocha/macos_version.rb +7 -0
- data/lib/mocha/method_matcher.rb +8 -10
- data/lib/mocha/minitest.rb +7 -0
- data/lib/mocha/mock.rb +333 -108
- data/lib/mocha/mockery.rb +192 -0
- data/lib/mocha/name.rb +13 -0
- data/lib/mocha/not_initialized_error.rb +9 -0
- data/lib/mocha/object_methods.rb +183 -0
- data/lib/mocha/object_receiver.rb +20 -0
- data/lib/mocha/parameter_matchers/all_of.rb +38 -28
- data/lib/mocha/parameter_matchers/any_of.rb +44 -33
- data/lib/mocha/parameter_matchers/any_parameters.rb +33 -26
- data/lib/mocha/parameter_matchers/anything.rb +31 -22
- data/lib/mocha/parameter_matchers/base_methods.rb +64 -0
- data/lib/mocha/parameter_matchers/equals.rb +36 -25
- data/lib/mocha/parameter_matchers/equivalent_uri.rb +65 -0
- data/lib/mocha/parameter_matchers/has_entries.rb +48 -29
- data/lib/mocha/parameter_matchers/has_entry.rb +90 -42
- data/lib/mocha/parameter_matchers/has_key.rb +39 -26
- data/lib/mocha/parameter_matchers/has_keys.rb +59 -0
- data/lib/mocha/parameter_matchers/has_value.rb +39 -26
- data/lib/mocha/parameter_matchers/includes.rb +88 -23
- data/lib/mocha/parameter_matchers/instance_methods.rb +28 -0
- data/lib/mocha/parameter_matchers/instance_of.rb +37 -26
- data/lib/mocha/parameter_matchers/is_a.rb +38 -26
- data/lib/mocha/parameter_matchers/kind_of.rb +39 -26
- data/lib/mocha/parameter_matchers/not.rb +37 -26
- data/lib/mocha/parameter_matchers/optionally.rb +52 -17
- data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +91 -0
- data/lib/mocha/parameter_matchers/regexp_matches.rb +37 -25
- data/lib/mocha/parameter_matchers/responds_with.rb +82 -0
- data/lib/mocha/parameter_matchers/yaml_equivalent.rb +55 -0
- data/lib/mocha/parameter_matchers.rb +12 -5
- data/lib/mocha/parameters_matcher.rb +28 -19
- data/lib/mocha/raised_exception.rb +13 -0
- data/lib/mocha/return_values.rb +13 -18
- data/lib/mocha/ruby_version.rb +7 -0
- data/lib/mocha/sequence.rb +23 -17
- data/lib/mocha/single_return_value.rb +8 -18
- data/lib/mocha/state_machine.rb +95 -0
- data/lib/mocha/stubbed_method.rb +96 -0
- data/lib/mocha/stubbing_error.rb +10 -0
- data/lib/mocha/test_unit.rb +7 -0
- data/lib/mocha/thrower.rb +15 -0
- data/lib/mocha/thrown_object.rb +14 -0
- data/lib/mocha/version.rb +5 -0
- data/lib/mocha/yield_parameters.rb +12 -20
- data/lib/mocha.rb +19 -17
- data/mocha.gemspec +40 -0
- metadata +129 -145
- data/COPYING +0 -3
- data/README +0 -35
- data/RELEASE +0 -188
- data/examples/misc.rb +0 -44
- data/examples/mocha.rb +0 -26
- data/examples/stubba.rb +0 -65
- data/lib/mocha/auto_verify.rb +0 -118
- data/lib/mocha/class_method.rb +0 -66
- data/lib/mocha/infinite_range.rb +0 -25
- data/lib/mocha/is_a.rb +0 -9
- data/lib/mocha/metaclass.rb +0 -7
- data/lib/mocha/missing_expectation.rb +0 -17
- data/lib/mocha/multiple_yields.rb +0 -20
- data/lib/mocha/no_yields.rb +0 -11
- data/lib/mocha/object.rb +0 -110
- data/lib/mocha/parameter_matchers/base.rb +0 -15
- data/lib/mocha/parameter_matchers/object.rb +0 -9
- data/lib/mocha/pretty_parameters.rb +0 -28
- data/lib/mocha/setup_and_teardown.rb +0 -23
- data/lib/mocha/single_yield.rb +0 -18
- data/lib/mocha/standalone.rb +0 -32
- data/lib/mocha/stub.rb +0 -18
- data/lib/mocha/test_case_adapter.rb +0 -49
- data/lib/mocha_standalone.rb +0 -2
- data/lib/stubba.rb +0 -2
- data/test/acceptance/expected_invocation_count_acceptance_test.rb +0 -187
- data/test/acceptance/mocha_acceptance_test.rb +0 -98
- data/test/acceptance/mock_with_initializer_block_acceptance_test.rb +0 -44
- data/test/acceptance/mocked_methods_dispatch_acceptance_test.rb +0 -71
- data/test/acceptance/optional_parameters_acceptance_test.rb +0 -63
- data/test/acceptance/parameter_matcher_acceptance_test.rb +0 -117
- data/test/acceptance/partial_mocks_acceptance_test.rb +0 -40
- data/test/acceptance/sequence_acceptance_test.rb +0 -179
- data/test/acceptance/standalone_acceptance_test.rb +0 -131
- data/test/acceptance/stubba_acceptance_test.rb +0 -102
- data/test/active_record_test_case.rb +0 -36
- data/test/deprecation_disabler.rb +0 -15
- data/test/execution_point.rb +0 -34
- data/test/integration/mocha_test_result_integration_test.rb +0 -105
- data/test/integration/stubba_integration_test.rb +0 -89
- data/test/integration/stubba_test_result_integration_test.rb +0 -85
- data/test/method_definer.rb +0 -18
- data/test/test_helper.rb +0 -12
- data/test/test_runner.rb +0 -31
- data/test/unit/any_instance_method_test.rb +0 -126
- data/test/unit/array_inspect_test.rb +0 -16
- data/test/unit/auto_verify_test.rb +0 -129
- data/test/unit/central_test.rb +0 -124
- data/test/unit/class_method_test.rb +0 -200
- data/test/unit/date_time_inspect_test.rb +0 -21
- data/test/unit/expectation_error_test.rb +0 -24
- data/test/unit/expectation_list_test.rb +0 -75
- data/test/unit/expectation_raiser_test.rb +0 -28
- data/test/unit/expectation_test.rb +0 -483
- data/test/unit/hash_inspect_test.rb +0 -16
- data/test/unit/infinite_range_test.rb +0 -53
- data/test/unit/metaclass_test.rb +0 -22
- data/test/unit/method_matcher_test.rb +0 -23
- data/test/unit/missing_expectation_test.rb +0 -42
- data/test/unit/mock_test.rb +0 -323
- data/test/unit/multiple_yields_test.rb +0 -18
- data/test/unit/no_yield_test.rb +0 -18
- data/test/unit/object_inspect_test.rb +0 -37
- data/test/unit/object_test.rb +0 -165
- data/test/unit/parameter_matchers/all_of_test.rb +0 -26
- data/test/unit/parameter_matchers/any_of_test.rb +0 -26
- data/test/unit/parameter_matchers/anything_test.rb +0 -21
- data/test/unit/parameter_matchers/has_entries_test.rb +0 -30
- data/test/unit/parameter_matchers/has_entry_test.rb +0 -40
- data/test/unit/parameter_matchers/has_key_test.rb +0 -25
- data/test/unit/parameter_matchers/has_value_test.rb +0 -25
- data/test/unit/parameter_matchers/includes_test.rb +0 -25
- data/test/unit/parameter_matchers/instance_of_test.rb +0 -25
- data/test/unit/parameter_matchers/is_a_test.rb +0 -25
- data/test/unit/parameter_matchers/kind_of_test.rb +0 -25
- data/test/unit/parameter_matchers/not_test.rb +0 -26
- data/test/unit/parameter_matchers/regexp_matches_test.rb +0 -25
- data/test/unit/parameter_matchers/stub_matcher.rb +0 -23
- data/test/unit/parameters_matcher_test.rb +0 -121
- data/test/unit/return_values_test.rb +0 -63
- data/test/unit/sequence_test.rb +0 -104
- data/test/unit/setup_and_teardown_test.rb +0 -76
- data/test/unit/single_return_value_test.rb +0 -33
- data/test/unit/single_yield_test.rb +0 -18
- data/test/unit/string_inspect_test.rb +0 -11
- data/test/unit/stub_test.rb +0 -24
- data/test/unit/yield_parameters_test.rb +0 -93
data/test/unit/mock_test.rb
DELETED
|
@@ -1,323 +0,0 @@
|
|
|
1
|
-
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
|
2
|
-
require 'mocha/mock'
|
|
3
|
-
require 'mocha/expectation_error'
|
|
4
|
-
require 'set'
|
|
5
|
-
|
|
6
|
-
class MockTest < Test::Unit::TestCase
|
|
7
|
-
|
|
8
|
-
include Mocha
|
|
9
|
-
|
|
10
|
-
def test_should_set_single_expectation
|
|
11
|
-
mock = Mock.new
|
|
12
|
-
mock.expects(:method1).returns(1)
|
|
13
|
-
assert_nothing_raised(ExpectationError) do
|
|
14
|
-
assert_equal 1, mock.method1
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def test_should_build_and_store_expectations
|
|
19
|
-
mock = Mock.new
|
|
20
|
-
expectation = mock.expects(:method1)
|
|
21
|
-
assert_not_nil expectation
|
|
22
|
-
assert_equal [expectation], mock.expectations.to_a
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def test_should_not_stub_everything_by_default
|
|
26
|
-
mock = Mock.new
|
|
27
|
-
assert_equal false, mock.everything_stubbed
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def test_should_stub_everything
|
|
31
|
-
mock = Mock.new
|
|
32
|
-
mock.stub_everything
|
|
33
|
-
assert_equal true, mock.everything_stubbed
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
def test_should_display_object_id_for_mocha_inspect_if_mock_has_no_name
|
|
37
|
-
mock = Mock.new
|
|
38
|
-
assert_match Regexp.new("^#<Mock:0x[0-9A-Fa-f]{1,12}>$"), mock.mocha_inspect
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def test_should_display_name_for_mocha_inspect_if_mock_has_name
|
|
42
|
-
mock = Mock.new('named_mock')
|
|
43
|
-
assert_equal "#<Mock:named_mock>", mock.mocha_inspect
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def test_should_display_object_id_for_inspect_if_mock_has_no_name
|
|
47
|
-
mock = Mock.new
|
|
48
|
-
assert_match Regexp.new("^#<Mock:0x[0-9A-Fa-f]{1,12}>$"), mock.inspect
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def test_should_display_name_for_inspect_if_mock_has_name
|
|
52
|
-
mock = Mock.new('named_mock')
|
|
53
|
-
assert_equal "#<Mock:named_mock>", mock.inspect
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
def test_should_be_able_to_extend_mock_object_with_module
|
|
57
|
-
mock = Mock.new
|
|
58
|
-
assert_nothing_raised(ExpectationError) { mock.extend(Module.new) }
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
def test_should_be_equal
|
|
62
|
-
mock = Mock.new
|
|
63
|
-
assert_equal true, mock.eql?(mock)
|
|
64
|
-
end
|
|
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
|
-
|
|
72
|
-
def test_should_be_able_to_mock_standard_object_methods
|
|
73
|
-
mock = Mock.new
|
|
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) }
|
|
76
|
-
assert_nothing_raised(ExpectationError) { mock.verify }
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
def test_should_be_able_to_stub_standard_object_methods
|
|
80
|
-
mock = Mock.new
|
|
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) }
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
def test_should_create_and_add_expectations
|
|
86
|
-
mock = Mock.new
|
|
87
|
-
expectation1 = mock.expects(:method1)
|
|
88
|
-
expectation2 = mock.expects(:method2)
|
|
89
|
-
assert_equal [expectation1, expectation2].to_set, mock.expectations.to_set
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
def test_should_pass_backtrace_into_expectation
|
|
93
|
-
mock = Mock.new
|
|
94
|
-
backtrace = Object.new
|
|
95
|
-
expectation = mock.expects(:method1, backtrace)
|
|
96
|
-
assert_equal backtrace, expectation.backtrace
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
def test_should_pass_backtrace_into_stub
|
|
100
|
-
mock = Mock.new
|
|
101
|
-
backtrace = Object.new
|
|
102
|
-
stub = mock.stubs(:method1, backtrace)
|
|
103
|
-
assert_equal backtrace, stub.backtrace
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
def test_should_create_and_add_stubs
|
|
107
|
-
mock = Mock.new
|
|
108
|
-
stub1 = mock.stubs(:method1)
|
|
109
|
-
stub2 = mock.stubs(:method2)
|
|
110
|
-
assert_equal [stub1, stub2].to_set, mock.expectations.to_set
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
def test_should_invoke_expectation_and_return_result
|
|
114
|
-
mock = Mock.new
|
|
115
|
-
mock.expects(:my_method).returns(:result)
|
|
116
|
-
result = mock.my_method
|
|
117
|
-
assert_equal :result, result
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
def test_should_not_raise_error_if_stubbing_everything
|
|
121
|
-
mock = Mock.new
|
|
122
|
-
mock.stub_everything
|
|
123
|
-
result = nil
|
|
124
|
-
assert_nothing_raised(ExpectationError) do
|
|
125
|
-
result = mock.unexpected_method
|
|
126
|
-
end
|
|
127
|
-
assert_nil result
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
def test_should_raise_assertion_error_for_unexpected_method_call
|
|
131
|
-
mock = Mock.new
|
|
132
|
-
error = assert_raise(ExpectationError) do
|
|
133
|
-
mock.unexpected_method_called(:my_method, :argument1, :argument2)
|
|
134
|
-
end
|
|
135
|
-
assert_match(/my_method/, error.message)
|
|
136
|
-
assert_match(/argument1/, error.message)
|
|
137
|
-
assert_match(/argument2/, error.message)
|
|
138
|
-
end
|
|
139
|
-
|
|
140
|
-
def test_should_indicate_unexpected_method_called
|
|
141
|
-
mock = Mock.new
|
|
142
|
-
class << mock
|
|
143
|
-
attr_accessor :symbol, :arguments
|
|
144
|
-
def unexpected_method_called(symbol, *arguments)
|
|
145
|
-
self.symbol, self.arguments = symbol, arguments
|
|
146
|
-
end
|
|
147
|
-
end
|
|
148
|
-
mock.my_method(:argument1, :argument2)
|
|
149
|
-
assert_equal :my_method, mock.symbol
|
|
150
|
-
assert_equal [:argument1, :argument2], mock.arguments
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
def test_should_verify_that_all_expectations_have_been_fulfilled
|
|
154
|
-
mock = Mock.new
|
|
155
|
-
mock.expects(:method1)
|
|
156
|
-
mock.expects(:method2)
|
|
157
|
-
mock.method1
|
|
158
|
-
assert_raise(ExpectationError) do
|
|
159
|
-
mock.verify
|
|
160
|
-
end
|
|
161
|
-
end
|
|
162
|
-
|
|
163
|
-
def test_should_report_possible_expectations
|
|
164
|
-
mock = Mock.new
|
|
165
|
-
mock.expects(:expected_method).with(1)
|
|
166
|
-
exception = assert_raise(ExpectationError) { mock.expected_method(2) }
|
|
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
|
|
168
|
-
end
|
|
169
|
-
|
|
170
|
-
def test_should_pass_block_through_to_expectations_verify_method
|
|
171
|
-
mock = Mock.new
|
|
172
|
-
expected_expectation = mock.expects(:method1)
|
|
173
|
-
mock.method1
|
|
174
|
-
expectations = []
|
|
175
|
-
mock.verify() { |expectation| expectations << expectation }
|
|
176
|
-
assert_equal [expected_expectation], expectations
|
|
177
|
-
end
|
|
178
|
-
|
|
179
|
-
def test_should_yield_supplied_parameters_to_block
|
|
180
|
-
mock = Mock.new
|
|
181
|
-
parameters_for_yield = [1, 2, 3]
|
|
182
|
-
mock.expects(:method1).yields(*parameters_for_yield)
|
|
183
|
-
yielded_parameters = nil
|
|
184
|
-
mock.method1() { |*parameters| yielded_parameters = parameters }
|
|
185
|
-
assert_equal parameters_for_yield, yielded_parameters
|
|
186
|
-
end
|
|
187
|
-
|
|
188
|
-
def test_should_set_up_multiple_expectations_with_return_values
|
|
189
|
-
mock = Mock.new
|
|
190
|
-
mock.expects(:method1 => :result1, :method2 => :result2)
|
|
191
|
-
assert_equal :result1, mock.method1
|
|
192
|
-
assert_equal :result2, mock.method2
|
|
193
|
-
end
|
|
194
|
-
|
|
195
|
-
def test_should_set_up_multiple_stubs_with_return_values
|
|
196
|
-
mock = Mock.new
|
|
197
|
-
mock.stubs(:method1 => :result1, :method2 => :result2)
|
|
198
|
-
assert_equal :result1, mock.method1
|
|
199
|
-
assert_equal :result2, mock.method2
|
|
200
|
-
end
|
|
201
|
-
|
|
202
|
-
def test_should_keep_returning_specified_value_for_stubs
|
|
203
|
-
mock = Mock.new
|
|
204
|
-
mock.stubs(:method1).returns(1)
|
|
205
|
-
assert_equal 1, mock.method1
|
|
206
|
-
assert_equal 1, mock.method1
|
|
207
|
-
end
|
|
208
|
-
|
|
209
|
-
def test_should_keep_returning_specified_value_for_expects
|
|
210
|
-
mock = Mock.new
|
|
211
|
-
mock.expects(:method1).times(2).returns(1)
|
|
212
|
-
assert_equal 1, mock.method1
|
|
213
|
-
assert_equal 1, mock.method1
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
def test_should_match_most_recent_call_to_expects
|
|
217
|
-
mock = Mock.new
|
|
218
|
-
mock.expects(:method1).returns(0)
|
|
219
|
-
mock.expects(:method1).returns(1)
|
|
220
|
-
assert_equal 1, mock.method1
|
|
221
|
-
end
|
|
222
|
-
|
|
223
|
-
def test_should_match_most_recent_call_to_stubs
|
|
224
|
-
mock = Mock.new
|
|
225
|
-
mock.stubs(:method1).returns(0)
|
|
226
|
-
mock.stubs(:method1).returns(1)
|
|
227
|
-
assert_equal 1, mock.method1
|
|
228
|
-
end
|
|
229
|
-
|
|
230
|
-
def test_should_match_most_recent_call_to_stubs_or_expects
|
|
231
|
-
mock = Mock.new
|
|
232
|
-
mock.stubs(:method1).returns(0)
|
|
233
|
-
mock.expects(:method1).returns(1)
|
|
234
|
-
assert_equal 1, mock.method1
|
|
235
|
-
end
|
|
236
|
-
|
|
237
|
-
def test_should_match_most_recent_call_to_expects_or_stubs
|
|
238
|
-
mock = Mock.new
|
|
239
|
-
mock.expects(:method1).returns(0)
|
|
240
|
-
mock.stubs(:method1).returns(1)
|
|
241
|
-
assert_equal 1, mock.method1
|
|
242
|
-
end
|
|
243
|
-
|
|
244
|
-
def test_should_respond_to_expected_method
|
|
245
|
-
mock = Mock.new
|
|
246
|
-
mock.expects(:method1)
|
|
247
|
-
assert_equal true, mock.respond_to?(:method1)
|
|
248
|
-
end
|
|
249
|
-
|
|
250
|
-
def test_should_not_respond_to_unexpected_method
|
|
251
|
-
mock = Mock.new
|
|
252
|
-
assert_equal false, mock.respond_to?(:method1)
|
|
253
|
-
end
|
|
254
|
-
|
|
255
|
-
def test_should_respond_to_methods_which_the_responder_does_responds_to
|
|
256
|
-
instance = Class.new do
|
|
257
|
-
define_method(:respond_to?) { |symbol| true }
|
|
258
|
-
end.new
|
|
259
|
-
mock = Mock.new
|
|
260
|
-
mock.responds_like(instance)
|
|
261
|
-
assert_equal true, mock.respond_to?(:invoked_method)
|
|
262
|
-
end
|
|
263
|
-
|
|
264
|
-
def test_should_not_respond_to_methods_which_the_responder_does_not_responds_to
|
|
265
|
-
instance = Class.new do
|
|
266
|
-
define_method(:respond_to?) { |symbol| false }
|
|
267
|
-
end.new
|
|
268
|
-
mock = Mock.new
|
|
269
|
-
mock.responds_like(instance)
|
|
270
|
-
assert_equal false, mock.respond_to?(:invoked_method)
|
|
271
|
-
end
|
|
272
|
-
|
|
273
|
-
def test_should_return_itself_to_allow_method_chaining
|
|
274
|
-
mock = Mock.new
|
|
275
|
-
assert_same mock.responds_like(Object.new), mock
|
|
276
|
-
end
|
|
277
|
-
|
|
278
|
-
def test_should_not_raise_no_method_error_if_mock_is_not_restricted_to_respond_like_a_responder
|
|
279
|
-
instance = Class.new do
|
|
280
|
-
define_method(:respond_to?) { true }
|
|
281
|
-
end.new
|
|
282
|
-
mock = Mock.new
|
|
283
|
-
mock.stubs(:invoked_method)
|
|
284
|
-
assert_nothing_raised(NoMethodError) { mock.invoked_method }
|
|
285
|
-
end
|
|
286
|
-
|
|
287
|
-
def test_should_not_raise_no_method_error_if_responder_does_respond_to_invoked_method
|
|
288
|
-
instance = Class.new do
|
|
289
|
-
define_method(:respond_to?) { |symbol| true }
|
|
290
|
-
end.new
|
|
291
|
-
mock = Mock.new
|
|
292
|
-
mock.responds_like(instance)
|
|
293
|
-
mock.stubs(:invoked_method)
|
|
294
|
-
assert_nothing_raised(NoMethodError) { mock.invoked_method }
|
|
295
|
-
end
|
|
296
|
-
|
|
297
|
-
def test_should_raise_no_method_error_if_responder_does_not_respond_to_invoked_method
|
|
298
|
-
instance = Class.new do
|
|
299
|
-
define_method(:respond_to?) { |symbol| false }
|
|
300
|
-
define_method(:mocha_inspect) { 'mocha_inspect' }
|
|
301
|
-
end.new
|
|
302
|
-
mock = Mock.new
|
|
303
|
-
mock.responds_like(instance)
|
|
304
|
-
mock.stubs(:invoked_method)
|
|
305
|
-
assert_raises(NoMethodError) { mock.invoked_method }
|
|
306
|
-
end
|
|
307
|
-
|
|
308
|
-
def test_should_raise_no_method_error_with_message_indicating_that_mock_is_constrained_to_respond_like_responder
|
|
309
|
-
instance = Class.new do
|
|
310
|
-
define_method(:respond_to?) { |symbol| false }
|
|
311
|
-
define_method(:mocha_inspect) { 'mocha_inspect' }
|
|
312
|
-
end.new
|
|
313
|
-
mock = Mock.new
|
|
314
|
-
mock.responds_like(instance)
|
|
315
|
-
mock.stubs(:invoked_method)
|
|
316
|
-
begin
|
|
317
|
-
mock.invoked_method
|
|
318
|
-
rescue NoMethodError => e
|
|
319
|
-
assert_match(/which responds like mocha_inspect/, e.message)
|
|
320
|
-
end
|
|
321
|
-
end
|
|
322
|
-
|
|
323
|
-
end
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
|
2
|
-
|
|
3
|
-
require 'mocha/multiple_yields'
|
|
4
|
-
|
|
5
|
-
class MultipleYieldsTest < Test::Unit::TestCase
|
|
6
|
-
|
|
7
|
-
include Mocha
|
|
8
|
-
|
|
9
|
-
def test_should_provide_parameters_for_multiple_yields_in_single_invocation
|
|
10
|
-
parameter_group = MultipleYields.new([1, 2, 3], [4, 5])
|
|
11
|
-
parameter_groups = []
|
|
12
|
-
parameter_group.each do |parameters|
|
|
13
|
-
parameter_groups << parameters
|
|
14
|
-
end
|
|
15
|
-
assert_equal [[1, 2, 3], [4, 5]], parameter_groups
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
end
|
data/test/unit/no_yield_test.rb
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
|
2
|
-
|
|
3
|
-
require 'mocha/no_yields'
|
|
4
|
-
|
|
5
|
-
class NoYieldsTest < Test::Unit::TestCase
|
|
6
|
-
|
|
7
|
-
include Mocha
|
|
8
|
-
|
|
9
|
-
def test_should_provide_parameters_for_no_yields_in_single_invocation
|
|
10
|
-
parameter_group = NoYields.new
|
|
11
|
-
parameter_groups = []
|
|
12
|
-
parameter_group.each do |parameters|
|
|
13
|
-
parameter_groups << parameters
|
|
14
|
-
end
|
|
15
|
-
assert_equal [], parameter_groups
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
end
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
|
2
|
-
require 'mocha/inspect'
|
|
3
|
-
require 'method_definer'
|
|
4
|
-
|
|
5
|
-
class ObjectInspectTest < Test::Unit::TestCase
|
|
6
|
-
|
|
7
|
-
def test_should_return_default_string_representation_of_object_not_including_instance_variables
|
|
8
|
-
object = Object.new
|
|
9
|
-
class << object
|
|
10
|
-
attr_accessor :attribute
|
|
11
|
-
end
|
|
12
|
-
object.attribute = 'instance_variable'
|
|
13
|
-
assert_match Regexp.new("^#<Object:0x[0-9A-Fa-f]{1,8}.*>$"), object.mocha_inspect
|
|
14
|
-
assert_no_match(/instance_variable/, object.mocha_inspect)
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def test_should_return_customized_string_representation_of_object
|
|
18
|
-
object = Object.new
|
|
19
|
-
class << object
|
|
20
|
-
define_method(:inspect) { 'custom_inspect' }
|
|
21
|
-
end
|
|
22
|
-
assert_equal 'custom_inspect', object.mocha_inspect
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def test_should_use_underscored_id_instead_of_object_id_or_id_so_that_they_can_be_stubbed
|
|
26
|
-
object = Object.new
|
|
27
|
-
object.define_instance_accessor(:called)
|
|
28
|
-
object.called = false
|
|
29
|
-
object.replace_instance_method(:object_id) { self.called = true; 1 }
|
|
30
|
-
if RUBY_VERSION < '1.9'
|
|
31
|
-
object.replace_instance_method(:id) { self.called = true; 1 }
|
|
32
|
-
end
|
|
33
|
-
object.mocha_inspect
|
|
34
|
-
assert_equal false, object.called
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
end
|
data/test/unit/object_test.rb
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
|
2
|
-
require 'mocha/mock'
|
|
3
|
-
require 'method_definer'
|
|
4
|
-
|
|
5
|
-
require 'mocha/object'
|
|
6
|
-
|
|
7
|
-
class ObjectTest < Test::Unit::TestCase
|
|
8
|
-
|
|
9
|
-
include Mocha
|
|
10
|
-
|
|
11
|
-
def test_should_build_mocha
|
|
12
|
-
instance = Object.new
|
|
13
|
-
mocha = instance.mocha
|
|
14
|
-
assert_not_nil mocha
|
|
15
|
-
assert mocha.is_a?(Mock)
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def test_should_reuse_existing_mocha
|
|
19
|
-
instance = Object.new
|
|
20
|
-
mocha_1 = instance.mocha
|
|
21
|
-
mocha_2 = instance.mocha
|
|
22
|
-
assert_equal mocha_1, mocha_2
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
def test_should_reset_mocha
|
|
26
|
-
instance = Object.new
|
|
27
|
-
assert_nil instance.reset_mocha
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def test_should_stub_instance_method
|
|
31
|
-
instance = Object.new
|
|
32
|
-
$stubba = Mock.new
|
|
33
|
-
$stubba.expects(:stub).with(Mocha::InstanceMethod.new(instance, :method1))
|
|
34
|
-
instance.expects(:method1)
|
|
35
|
-
$stubba.verify
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def test_should_build_and_store_expectation
|
|
39
|
-
instance = Object.new
|
|
40
|
-
$stubba = Mock.new
|
|
41
|
-
$stubba.stubs(:stub)
|
|
42
|
-
expectation = instance.expects(:method1)
|
|
43
|
-
assert_equal [expectation], instance.mocha.expectations.to_a
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def test_should_verify_expectations
|
|
47
|
-
instance = Object.new
|
|
48
|
-
$stubba = Mock.new
|
|
49
|
-
$stubba.stubs(:stub)
|
|
50
|
-
instance.expects(:method1).with(:value1, :value2)
|
|
51
|
-
assert_raise(ExpectationError) { instance.verify }
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def test_should_pass_backtrace_into_expects
|
|
55
|
-
instance = Object.new
|
|
56
|
-
$stubba = Mock.new
|
|
57
|
-
$stubba.stubs(:stub)
|
|
58
|
-
mocha = Object.new
|
|
59
|
-
mocha.define_instance_accessor(:expects_parameters)
|
|
60
|
-
mocha.define_instance_method(:expects) { |*parameters| self.expects_parameters = parameters }
|
|
61
|
-
backtrace = Object.new
|
|
62
|
-
instance.define_instance_method(:mocha) { mocha }
|
|
63
|
-
instance.define_instance_method(:caller) { backtrace }
|
|
64
|
-
instance.expects(:method1)
|
|
65
|
-
assert_equal [:method1, backtrace], mocha.expects_parameters
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
def test_should_pass_backtrace_into_stubs
|
|
69
|
-
instance = Object.new
|
|
70
|
-
$stubba = Mock.new
|
|
71
|
-
$stubba.stubs(:stub)
|
|
72
|
-
mocha = Object.new
|
|
73
|
-
mocha.define_instance_accessor(:stubs_parameters)
|
|
74
|
-
mocha.define_instance_method(:stubs) { |*parameters| self.stubs_parameters = parameters }
|
|
75
|
-
backtrace = Object.new
|
|
76
|
-
instance.define_instance_method(:mocha) { mocha }
|
|
77
|
-
instance.define_instance_method(:caller) { backtrace }
|
|
78
|
-
instance.stubs(:method1)
|
|
79
|
-
assert_equal [:method1, backtrace], mocha.stubs_parameters
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
def test_should_build_any_instance_object
|
|
83
|
-
klass = Class.new
|
|
84
|
-
any_instance = klass.any_instance
|
|
85
|
-
assert_not_nil any_instance
|
|
86
|
-
assert any_instance.is_a?(Class::AnyInstance)
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
def test_should_return_same_any_instance_object
|
|
90
|
-
klass = Class.new
|
|
91
|
-
any_instance_1 = klass.any_instance
|
|
92
|
-
any_instance_2 = klass.any_instance
|
|
93
|
-
assert_equal any_instance_1, any_instance_2
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
def test_should_stub_class_method
|
|
97
|
-
klass = Class.new
|
|
98
|
-
$stubba = Mock.new
|
|
99
|
-
$stubba.expects(:stub).with(Mocha::ClassMethod.new(klass, :method1))
|
|
100
|
-
klass.expects(:method1)
|
|
101
|
-
$stubba.verify
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
def test_should_build_and_store_class_method_expectation
|
|
105
|
-
klass = Class.new
|
|
106
|
-
$stubba = Mock.new
|
|
107
|
-
$stubba.stubs(:stub)
|
|
108
|
-
expectation = klass.expects(:method1)
|
|
109
|
-
assert_equal [expectation], klass.mocha.expectations.to_a
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
def test_should_stub_module_method
|
|
113
|
-
mod = Module.new
|
|
114
|
-
$stubba = Mock.new
|
|
115
|
-
$stubba.expects(:stub).with(Mocha::ClassMethod.new(mod, :method1))
|
|
116
|
-
mod.expects(:method1)
|
|
117
|
-
$stubba.verify
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
def test_should_build_and_store_module_method_expectation
|
|
121
|
-
mod = Module.new
|
|
122
|
-
$stubba = Mock.new
|
|
123
|
-
$stubba.stubs(:stub)
|
|
124
|
-
expectation = mod.expects(:method1)
|
|
125
|
-
assert_equal [expectation], mod.mocha.expectations.to_a
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
def test_should_use_stubba_instance_method_for_object
|
|
129
|
-
assert_equal Mocha::InstanceMethod, Object.new.stubba_method
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
def test_should_use_stubba_class_method_for_module
|
|
133
|
-
assert_equal Mocha::ClassMethod, Module.new.stubba_method
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
def test_should_use_stubba_class_method_for_class
|
|
137
|
-
assert_equal Mocha::ClassMethod, Class.new.stubba_method
|
|
138
|
-
end
|
|
139
|
-
|
|
140
|
-
def test_should_use_stubba_class_method_for_any_instance
|
|
141
|
-
assert_equal Mocha::AnyInstanceMethod, Class::AnyInstance.new(nil).stubba_method
|
|
142
|
-
end
|
|
143
|
-
|
|
144
|
-
def test_should_stub_self_for_object
|
|
145
|
-
object = Object.new
|
|
146
|
-
assert_equal object, object.stubba_object
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
def test_should_stub_self_for_module
|
|
150
|
-
mod = Module.new
|
|
151
|
-
assert_equal mod, mod.stubba_object
|
|
152
|
-
end
|
|
153
|
-
|
|
154
|
-
def test_should_stub_self_for_class
|
|
155
|
-
klass = Class.new
|
|
156
|
-
assert_equal klass, klass.stubba_object
|
|
157
|
-
end
|
|
158
|
-
|
|
159
|
-
def test_should_stub_relevant_class_for_any_instance
|
|
160
|
-
klass = Class.new
|
|
161
|
-
any_instance = Class::AnyInstance.new(klass)
|
|
162
|
-
assert_equal klass, any_instance.stubba_object
|
|
163
|
-
end
|
|
164
|
-
|
|
165
|
-
end
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
|
|
2
|
-
|
|
3
|
-
require 'mocha/parameter_matchers/all_of'
|
|
4
|
-
require 'mocha/inspect'
|
|
5
|
-
require 'stub_matcher'
|
|
6
|
-
|
|
7
|
-
class AllOfTest < Test::Unit::TestCase
|
|
8
|
-
|
|
9
|
-
include Mocha::ParameterMatchers
|
|
10
|
-
|
|
11
|
-
def test_should_match_if_all_matchers_match
|
|
12
|
-
matcher = all_of(Stub::Matcher.new(true), Stub::Matcher.new(true), Stub::Matcher.new(true))
|
|
13
|
-
assert matcher.matches?(['any_old_value'])
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def test_should_not_match_if_any_matcher_does_not_match
|
|
17
|
-
matcher = all_of(Stub::Matcher.new(true), Stub::Matcher.new(false), Stub::Matcher.new(true))
|
|
18
|
-
assert !matcher.matches?(['any_old_value'])
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
def test_should_describe_matcher
|
|
22
|
-
matcher = all_of(Stub::Matcher.new(true), Stub::Matcher.new(false), Stub::Matcher.new(true))
|
|
23
|
-
assert_equal 'all_of(matcher(true), matcher(false), matcher(true))', matcher.mocha_inspect
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
end
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
|
|
2
|
-
|
|
3
|
-
require 'mocha/parameter_matchers/any_of'
|
|
4
|
-
require 'mocha/inspect'
|
|
5
|
-
require 'stub_matcher'
|
|
6
|
-
|
|
7
|
-
class AnyOfTest < Test::Unit::TestCase
|
|
8
|
-
|
|
9
|
-
include Mocha::ParameterMatchers
|
|
10
|
-
|
|
11
|
-
def test_should_match_if_any_matchers_match
|
|
12
|
-
matcher = any_of(Stub::Matcher.new(false), Stub::Matcher.new(true), Stub::Matcher.new(false))
|
|
13
|
-
assert matcher.matches?(['any_old_value'])
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def test_should_not_match_if_no_matchers_match
|
|
17
|
-
matcher = any_of(Stub::Matcher.new(false), Stub::Matcher.new(false), Stub::Matcher.new(false))
|
|
18
|
-
assert !matcher.matches?(['any_old_value'])
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
def test_should_describe_matcher
|
|
22
|
-
matcher = any_of(Stub::Matcher.new(false), Stub::Matcher.new(true), Stub::Matcher.new(false))
|
|
23
|
-
assert_equal 'any_of(matcher(false), matcher(true), matcher(false))', matcher.mocha_inspect
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
end
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
|
|
2
|
-
|
|
3
|
-
require 'mocha/parameter_matchers/anything'
|
|
4
|
-
require 'mocha/inspect'
|
|
5
|
-
|
|
6
|
-
class AnythingTest < Test::Unit::TestCase
|
|
7
|
-
|
|
8
|
-
include Mocha::ParameterMatchers
|
|
9
|
-
|
|
10
|
-
def test_should_match_anything
|
|
11
|
-
matcher = anything
|
|
12
|
-
assert matcher.matches?([:something])
|
|
13
|
-
assert matcher.matches?([{'x' => 'y'}])
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def test_should_describe_matcher
|
|
17
|
-
matcher = anything
|
|
18
|
-
assert_equal "anything", matcher.mocha_inspect
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
end
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
|
|
2
|
-
|
|
3
|
-
require 'mocha/parameter_matchers/has_entries'
|
|
4
|
-
require 'mocha/inspect'
|
|
5
|
-
|
|
6
|
-
class HasEntriesTest < Test::Unit::TestCase
|
|
7
|
-
|
|
8
|
-
include Mocha::ParameterMatchers
|
|
9
|
-
|
|
10
|
-
def test_should_match_hash_including_specified_entries
|
|
11
|
-
matcher = has_entries(:key_1 => 'value_1', :key_2 => 'value_2')
|
|
12
|
-
assert matcher.matches?([{ :key_1 => 'value_1', :key_2 => 'value_2', :key_3 => 'value_3' }])
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def test_should_not_match_hash_not_including_specified_entries
|
|
16
|
-
matcher = has_entries(:key_1 => 'value_2', :key_2 => 'value_2', :key_3 => 'value_3')
|
|
17
|
-
assert !matcher.matches?([{ :key_1 => 'value_1', :key_2 => 'value_2' }])
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def test_should_describe_matcher
|
|
21
|
-
matcher = has_entries(:key_1 => 'value_1', :key_2 => 'value_2')
|
|
22
|
-
description = matcher.mocha_inspect
|
|
23
|
-
matches = /has_entries\((.*)\)/.match(description)
|
|
24
|
-
assert_not_nil matches[0]
|
|
25
|
-
entries = eval(matches[1])
|
|
26
|
-
assert_equal 'value_1', entries[:key_1]
|
|
27
|
-
assert_equal 'value_2', entries[:key_2]
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
end
|