mocha 1.9.0 → 1.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/.rubocop_todo.yml +7 -30
- data/README.md +30 -11
- data/RELEASE.md +58 -0
- data/Rakefile +9 -3
- data/docs/CNAME +1 -0
- data/docs/Mocha.html +132 -5
- data/docs/Mocha/API.html +313 -216
- data/docs/Mocha/ClassMethods.html +13 -16
- data/docs/Mocha/Configuration.html +931 -174
- data/docs/Mocha/Expectation.html +200 -255
- data/docs/Mocha/ExpectationError.html +5 -10
- data/docs/Mocha/ExpectationErrorFactory.html +9 -18
- data/docs/Mocha/Hooks.html +12 -27
- data/docs/Mocha/Integration.html +3 -3
- data/docs/Mocha/Integration/MiniTest.html +3 -3
- data/docs/Mocha/Integration/MiniTest/Adapter.html +5 -6
- data/docs/Mocha/Integration/TestUnit.html +3 -3
- data/docs/Mocha/Integration/TestUnit/Adapter.html +5 -6
- data/docs/Mocha/Mock.html +202 -162
- data/docs/Mocha/ObjectMethods.html +121 -68
- data/docs/Mocha/ParameterMatchers.html +21 -109
- data/docs/Mocha/ParameterMatchers/AllOf.html +4 -5
- data/docs/Mocha/ParameterMatchers/AnyOf.html +4 -5
- data/docs/Mocha/ParameterMatchers/AnyParameters.html +3 -3
- data/docs/Mocha/ParameterMatchers/Anything.html +3 -3
- data/docs/Mocha/ParameterMatchers/Base.html +8 -15
- data/docs/Mocha/ParameterMatchers/Equals.html +4 -5
- data/docs/Mocha/ParameterMatchers/EquivalentUri.html +3 -3
- data/docs/Mocha/ParameterMatchers/HasEntries.html +4 -5
- data/docs/Mocha/ParameterMatchers/HasEntry.html +4 -5
- data/docs/Mocha/ParameterMatchers/HasKey.html +4 -5
- data/docs/Mocha/ParameterMatchers/HasValue.html +4 -5
- data/docs/Mocha/ParameterMatchers/Includes.html +4 -5
- data/docs/Mocha/ParameterMatchers/InstanceOf.html +4 -5
- data/docs/Mocha/ParameterMatchers/IsA.html +3 -3
- data/docs/Mocha/ParameterMatchers/KindOf.html +4 -5
- data/docs/Mocha/ParameterMatchers/Not.html +4 -5
- data/docs/Mocha/ParameterMatchers/Optionally.html +3 -3
- data/docs/Mocha/ParameterMatchers/RegexpMatches.html +4 -5
- data/docs/Mocha/ParameterMatchers/RespondsWith.html +4 -5
- data/docs/Mocha/ParameterMatchers/YamlEquivalent.html +4 -5
- data/docs/Mocha/Sequence.html +3 -3
- data/docs/Mocha/StateMachine.html +13 -25
- data/docs/Mocha/StateMachine/State.html +4 -5
- data/docs/Mocha/StateMachine/StatePredicate.html +4 -5
- data/docs/Mocha/StubbingError.html +3 -3
- data/docs/_index.html +4 -22
- data/docs/class_list.html +1 -1
- data/docs/file.COPYING.html +3 -3
- data/docs/file.MIT-LICENSE.html +3 -3
- data/docs/file.README.html +36 -14
- data/docs/file.RELEASE.html +72 -3
- data/docs/frames.html +1 -1
- data/docs/index.html +36 -14
- data/docs/method_list.html +96 -40
- data/docs/top-level-namespace.html +3 -3
- data/gemfiles/Gemfile.minitest.5.11.3 +7 -0
- data/init.rb +1 -3
- data/lib/mocha.rb +8 -0
- data/lib/mocha/any_instance_method.rb +9 -25
- data/lib/mocha/api.rb +65 -66
- data/lib/mocha/cardinality.rb +26 -11
- data/lib/mocha/class_methods.rb +17 -15
- data/lib/mocha/configuration.rb +295 -67
- data/lib/mocha/detection/test_unit.rb +1 -3
- data/lib/mocha/exception_raiser.rb +2 -1
- data/lib/mocha/expectation.rb +56 -58
- data/lib/mocha/expectation_error.rb +1 -3
- data/lib/mocha/expectation_list.rb +6 -6
- data/lib/mocha/inspect.rb +28 -26
- data/lib/mocha/instance_method.rb +19 -2
- data/lib/mocha/integration.rb +1 -3
- data/lib/mocha/integration/mini_test.rb +7 -0
- data/lib/mocha/integration/test_unit.rb +7 -0
- data/lib/mocha/invocation.rb +61 -0
- data/lib/mocha/macos_version.rb +5 -0
- data/lib/mocha/minitest.rb +6 -1
- data/lib/mocha/mock.rb +46 -31
- data/lib/mocha/mockery.rb +28 -61
- data/lib/mocha/names.rb +1 -1
- data/lib/mocha/object_methods.rb +13 -19
- data/lib/mocha/parameter_matchers.rb +1 -1
- data/lib/mocha/parameter_matchers/all_of.rb +1 -1
- data/lib/mocha/parameter_matchers/any_of.rb +1 -1
- data/lib/mocha/parameter_matchers/equivalent_uri.rb +0 -9
- data/lib/mocha/parameter_matchers/includes.rb +2 -0
- data/lib/mocha/parameter_matchers/instance_methods.rb +18 -0
- data/lib/mocha/raised_exception.rb +11 -0
- data/lib/mocha/return_values.rb +3 -3
- data/lib/mocha/setup.rb +5 -0
- data/lib/mocha/single_return_value.rb +2 -1
- data/lib/mocha/singleton_class.rb +9 -0
- data/lib/mocha/{class_method.rb → stubbed_method.rb} +23 -43
- data/lib/mocha/test_unit.rb +6 -1
- data/lib/mocha/thrower.rb +2 -1
- data/lib/mocha/thrown_object.rb +12 -0
- data/lib/mocha/version.rb +1 -1
- data/mocha.gemspec +1 -3
- data/test/acceptance/acceptance_test_helper.rb +6 -0
- data/test/acceptance/bug_18914_test.rb +0 -1
- data/test/acceptance/bug_21465_test.rb +0 -1
- data/test/acceptance/bug_21563_test.rb +0 -1
- data/test/acceptance/display_matching_invocations_alongside_expectations_test.rb +69 -0
- data/test/acceptance/exception_rescue_test.rb +1 -2
- data/test/acceptance/expectations_on_multiple_methods_test.rb +0 -1
- data/test/acceptance/expected_invocation_count_test.rb +2 -3
- data/test/acceptance/failure_messages_test.rb +0 -1
- data/test/acceptance/issue_272_test.rb +1 -2
- data/test/acceptance/issue_65_test.rb +0 -1
- data/test/acceptance/issue_70_test.rb +0 -1
- data/test/acceptance/mocha_example_test.rb +0 -1
- data/test/acceptance/mocha_test_result_test.rb +0 -1
- data/test/acceptance/mock_test.rb +47 -6
- data/test/acceptance/mocked_methods_dispatch_test.rb +0 -1
- data/test/acceptance/multiple_expectations_failure_message_test.rb +0 -1
- data/test/acceptance/optional_parameters_test.rb +0 -1
- data/test/acceptance/parameter_matcher_test.rb +0 -1
- data/test/acceptance/partial_mocks_test.rb +0 -1
- data/test/acceptance/prepend_test.rb +0 -1
- data/test/acceptance/prevent_use_of_mocha_outside_test_test.rb +0 -1
- data/test/acceptance/raise_exception_test.rb +0 -1
- data/test/acceptance/return_value_test.rb +0 -1
- data/test/acceptance/sequence_test.rb +0 -1
- data/test/acceptance/states_test.rb +0 -1
- data/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb +1 -2
- data/test/acceptance/stub_any_instance_method_test.rb +20 -1
- data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +0 -1
- data/test/acceptance/stub_class_method_defined_on_class_test.rb +0 -1
- data/test/acceptance/stub_class_method_defined_on_module_test.rb +0 -1
- data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +1 -2
- data/test/acceptance/stub_everything_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_class_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_module_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -1
- data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -1
- data/test/acceptance/stub_method_defined_on_module_and_aliased_test.rb +0 -1
- data/test/acceptance/stub_module_method_test.rb +0 -1
- data/test/acceptance/stub_test.rb +0 -1
- data/test/acceptance/stubba_example_test.rb +0 -1
- data/test/acceptance/stubba_test_result_test.rb +0 -1
- data/test/acceptance/stubbing_error_backtrace_test.rb +4 -5
- data/test/acceptance/stubbing_frozen_object_test.rb +0 -1
- data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +0 -1
- data/test/acceptance/stubbing_method_unnecessarily_test.rb +5 -5
- data/test/acceptance/stubbing_nil_test.rb +5 -5
- data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +11 -11
- data/test/acceptance/stubbing_non_existent_class_method_test.rb +11 -11
- data/test/acceptance/stubbing_non_existent_instance_method_test.rb +11 -11
- data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +8 -8
- data/test/acceptance/stubbing_non_public_class_method_test.rb +9 -9
- data/test/acceptance/stubbing_non_public_instance_method_test.rb +9 -9
- data/test/acceptance/stubbing_on_non_mock_object_test.rb +5 -5
- data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +0 -1
- data/test/acceptance/throw_test.rb +0 -1
- data/test/acceptance/unexpected_invocation_test.rb +0 -1
- data/test/acceptance/unstubbing_test.rb +0 -1
- data/test/integration/shared_tests.rb +5 -3
- data/test/method_definer.rb +11 -17
- data/test/test_runner.rb +2 -0
- data/test/unit/any_instance_method_test.rb +41 -40
- data/test/unit/cardinality_test.rb +41 -23
- data/test/unit/central_test.rb +0 -1
- data/test/unit/class_methods_test.rb +1 -1
- data/test/unit/configuration_test.rb +12 -12
- data/test/unit/exception_raiser_test.rb +10 -5
- data/test/unit/expectation_list_test.rb +13 -11
- data/test/unit/expectation_test.rb +88 -103
- data/test/unit/instance_method_test.rb +282 -0
- data/test/unit/mock_test.rb +28 -19
- data/test/unit/mockery_test.rb +8 -11
- data/test/unit/module_methods_test.rb +2 -3
- data/test/unit/object_inspect_test.rb +6 -4
- data/test/unit/object_methods_test.rb +3 -2
- data/test/unit/parameter_matchers/equivalent_uri_test.rb +0 -9
- data/test/unit/parameter_matchers/has_entries_test.rb +1 -1
- data/test/unit/parameter_matchers/has_entry_test.rb +1 -1
- data/test/unit/parameter_matchers/has_key_test.rb +1 -1
- data/test/unit/parameter_matchers/has_value_test.rb +1 -1
- data/test/unit/parameter_matchers/includes_test.rb +1 -1
- data/test/unit/parameter_matchers/responds_with_test.rb +1 -1
- data/test/unit/return_values_test.rb +25 -20
- data/test/unit/single_return_value_test.rb +6 -1
- data/test/unit/thrower_test.rb +7 -2
- metadata +14 -28
- data/docs/Mocha/UnexpectedInvocation.html +0 -140
- data/lib/mocha/mini_test.rb +0 -5
- data/lib/mocha/module_method.rb +0 -6
- data/lib/mocha/module_methods.rb +0 -10
- data/lib/mocha/parameter_matchers/object.rb +0 -15
- data/lib/mocha/standalone.rb +0 -4
- data/lib/mocha/unexpected_invocation.rb +0 -24
- data/lib/mocha_standalone.rb +0 -4
- data/test/acceptance/mock_with_initializer_block_test.rb +0 -56
- data/test/unit/class_method_test.rb +0 -276
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'test_runner'
|
2
2
|
require 'execution_point'
|
3
3
|
|
4
|
+
# rubocop:disable Metrics/ModuleLength
|
4
5
|
module SharedTests
|
5
6
|
include TestRunner
|
6
7
|
|
@@ -63,7 +64,7 @@ module SharedTests
|
|
63
64
|
assert_equal [
|
64
65
|
'not all expectations were satisfied',
|
65
66
|
'unsatisfied expectations:',
|
66
|
-
'- expected exactly once,
|
67
|
+
'- expected exactly once, invoked never: #<Mock:expecting invocation>.expected(any_parameters)'
|
67
68
|
], test_result.failure_message_lines
|
68
69
|
end
|
69
70
|
|
@@ -101,7 +102,7 @@ module SharedTests
|
|
101
102
|
assert_equal [
|
102
103
|
'not all expectations were satisfied',
|
103
104
|
'unsatisfied expectations:',
|
104
|
-
'- expected exactly once,
|
105
|
+
'- expected exactly once, invoked never: #<Mock:expecting invocation>.expected(any_parameters)'
|
105
106
|
], test_result.failure_message_lines
|
106
107
|
end
|
107
108
|
|
@@ -151,7 +152,7 @@ module SharedTests
|
|
151
152
|
assert_equal [
|
152
153
|
'not all expectations were satisfied',
|
153
154
|
'unsatisfied expectations:',
|
154
|
-
"- expected exactly once,
|
155
|
+
"- expected exactly once, invoked never: #{object.mocha_inspect}.expected(any_parameters)"
|
155
156
|
], test_result.failure_message_lines
|
156
157
|
end
|
157
158
|
|
@@ -174,3 +175,4 @@ module SharedTests
|
|
174
175
|
end
|
175
176
|
# rubocop:enable Style/Semicolon
|
176
177
|
end
|
178
|
+
# rubocop:enable Metrics/ModuleLength
|
data/test/method_definer.rb
CHANGED
@@ -1,22 +1,16 @@
|
|
1
|
-
require '
|
1
|
+
require 'mocha/singleton_class'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
8
|
-
|
9
|
-
def replace_instance_method(method_symbol, &block)
|
10
|
-
raise "Cannot replace #{method_symbol} as #{self} does not respond to it." unless respond_to?(method_symbol)
|
11
|
-
define_instance_method(method_symbol, &block)
|
12
|
-
end
|
3
|
+
module MethodDefiner
|
4
|
+
def define_instance_method(object, method_symbol, &block)
|
5
|
+
object.singleton_class.send(:define_method, method_symbol, block)
|
6
|
+
end
|
13
7
|
|
14
|
-
|
15
|
-
|
16
|
-
|
8
|
+
def replace_instance_method(object, method_symbol, &block)
|
9
|
+
raise "Cannot replace #{method_symbol} as #{self} does not respond to it." unless object.respond_to?(method_symbol)
|
10
|
+
define_instance_method(object, method_symbol, &block)
|
17
11
|
end
|
18
|
-
end
|
19
12
|
|
20
|
-
|
21
|
-
|
13
|
+
def define_instance_accessor(object, *symbols)
|
14
|
+
symbols.each { |symbol| object.singleton_class.send(:attr_accessor, symbol) }
|
15
|
+
end
|
22
16
|
end
|
data/test/test_runner.rb
CHANGED
@@ -7,6 +7,7 @@ module TestRunner
|
|
7
7
|
run_as_tests(:test_me => block)
|
8
8
|
end
|
9
9
|
|
10
|
+
# rubocop:disable Metrics/AbcSize
|
10
11
|
def run_as_tests(methods = {})
|
11
12
|
base_class = Mocha::TestCase
|
12
13
|
test_class = Class.new(base_class) do
|
@@ -44,6 +45,7 @@ module TestRunner
|
|
44
45
|
|
45
46
|
test_result
|
46
47
|
end
|
48
|
+
# rubocop:enable Metrics/AbcSize
|
47
49
|
|
48
50
|
def assert_passed(test_result)
|
49
51
|
flunk "Test failed unexpectedly with message: #{test_result.failures}" if test_result.failure_count > 0
|
@@ -1,14 +1,23 @@
|
|
1
1
|
require File.expand_path('../../test_helper', __FILE__)
|
2
2
|
require 'method_definer'
|
3
|
+
require 'mocha/class_methods'
|
3
4
|
require 'mocha/mock'
|
4
5
|
require 'mocha/any_instance_method'
|
5
6
|
|
6
7
|
class AnyInstanceMethodTest < Mocha::TestCase
|
8
|
+
include MethodDefiner
|
7
9
|
include Mocha
|
8
10
|
|
11
|
+
def class_with_method(method, result = nil)
|
12
|
+
Class.new do
|
13
|
+
extend ClassMethods
|
14
|
+
define_method(method) { result } if method
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
9
18
|
unless RUBY_V2_PLUS
|
10
19
|
def test_should_hide_original_method
|
11
|
-
klass =
|
20
|
+
klass = class_with_method(:method_x)
|
12
21
|
method = AnyInstanceMethod.new(klass, :method_x)
|
13
22
|
|
14
23
|
method.hide_original_method
|
@@ -18,20 +27,20 @@ class AnyInstanceMethodTest < Mocha::TestCase
|
|
18
27
|
end
|
19
28
|
|
20
29
|
def test_should_not_raise_error_hiding_method_that_isnt_defined
|
21
|
-
klass =
|
30
|
+
klass = class_with_method(:irrelevant)
|
22
31
|
method = AnyInstanceMethod.new(klass, :method_x)
|
23
32
|
|
24
33
|
assert_nothing_raised { method.hide_original_method }
|
25
34
|
end
|
26
35
|
|
27
36
|
def test_should_define_a_new_method
|
28
|
-
klass =
|
37
|
+
klass = class_with_method(:method_x)
|
29
38
|
method = AnyInstanceMethod.new(klass, :method_x)
|
30
39
|
mocha = build_mock
|
31
40
|
mocha.expects(:method_x).with(:param1, :param2).returns(:result)
|
32
41
|
any_instance = Object.new
|
33
|
-
|
34
|
-
|
42
|
+
define_instance_method(any_instance, :mocha) { mocha }
|
43
|
+
define_instance_method(klass, :any_instance) { any_instance }
|
35
44
|
|
36
45
|
method.hide_original_method
|
37
46
|
method.define_new_method
|
@@ -44,19 +53,19 @@ class AnyInstanceMethodTest < Mocha::TestCase
|
|
44
53
|
end
|
45
54
|
|
46
55
|
def test_should_include_the_filename_and_line_number_in_exceptions
|
47
|
-
klass =
|
56
|
+
klass = class_with_method(:method_x)
|
48
57
|
method = AnyInstanceMethod.new(klass, :method_x)
|
49
58
|
mocha = build_mock
|
50
59
|
mocha.stubs(:method_x).raises(Exception)
|
51
60
|
any_instance = Object.new
|
52
|
-
|
53
|
-
|
61
|
+
define_instance_method(any_instance, :mocha) { mocha }
|
62
|
+
define_instance_method(klass, :any_instance) { any_instance }
|
54
63
|
|
55
64
|
method.hide_original_method
|
56
65
|
method.define_new_method
|
57
66
|
|
58
|
-
expected_filename = '
|
59
|
-
expected_line_number =
|
67
|
+
expected_filename = 'stubbed_method.rb'
|
68
|
+
expected_line_number = 61
|
60
69
|
|
61
70
|
exception = assert_raises(Exception) { klass.new.method_x }
|
62
71
|
matching_line = exception.backtrace.find do |line|
|
@@ -68,11 +77,7 @@ class AnyInstanceMethodTest < Mocha::TestCase
|
|
68
77
|
end
|
69
78
|
|
70
79
|
def test_should_restore_original_method
|
71
|
-
klass =
|
72
|
-
def method_x
|
73
|
-
:original_result
|
74
|
-
end
|
75
|
-
end
|
80
|
+
klass = class_with_method(:method_x, :original_result)
|
76
81
|
method = AnyInstanceMethod.new(klass, :method_x)
|
77
82
|
|
78
83
|
method.hide_original_method
|
@@ -86,11 +91,7 @@ class AnyInstanceMethodTest < Mocha::TestCase
|
|
86
91
|
end
|
87
92
|
|
88
93
|
def test_should_not_restore_original_method_if_none_was_defined_in_first_place
|
89
|
-
klass =
|
90
|
-
def method_x
|
91
|
-
:new_result
|
92
|
-
end
|
93
|
-
end
|
94
|
+
klass = class_with_method(:method_x, :new_result)
|
94
95
|
method = AnyInstanceMethod.new(klass, :method_x)
|
95
96
|
|
96
97
|
method.restore_original_method
|
@@ -100,16 +101,16 @@ class AnyInstanceMethodTest < Mocha::TestCase
|
|
100
101
|
end
|
101
102
|
|
102
103
|
def test_should_call_remove_new_method
|
103
|
-
klass =
|
104
|
+
klass = class_with_method(:method_x)
|
104
105
|
any_instance = build_mock
|
105
106
|
any_instance_mocha = build_mock
|
106
107
|
any_instance.stubs(:mocha).returns(any_instance_mocha)
|
107
|
-
|
108
|
+
define_instance_method(klass, :any_instance) { any_instance }
|
108
109
|
method = AnyInstanceMethod.new(klass, :method_x)
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
110
|
+
replace_instance_method(method, :restore_original_method) {}
|
111
|
+
replace_instance_method(method, :reset_mocha) {}
|
112
|
+
define_instance_accessor(method, :remove_called)
|
113
|
+
replace_instance_method(method, :remove_new_method) { self.remove_called = true }
|
113
114
|
|
114
115
|
method.unstub
|
115
116
|
|
@@ -117,16 +118,16 @@ class AnyInstanceMethodTest < Mocha::TestCase
|
|
117
118
|
end
|
118
119
|
|
119
120
|
def test_should_call_restore_original_method
|
120
|
-
klass =
|
121
|
+
klass = class_with_method(:method_x)
|
121
122
|
any_instance = build_mock
|
122
123
|
any_instance_mocha = build_mock
|
123
124
|
any_instance.stubs(:mocha).returns(any_instance_mocha)
|
124
|
-
|
125
|
+
define_instance_method(klass, :any_instance) { any_instance }
|
125
126
|
method = AnyInstanceMethod.new(klass, :method_x)
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
127
|
+
replace_instance_method(method, :remove_new_method) {}
|
128
|
+
replace_instance_method(method, :reset_mocha) {}
|
129
|
+
define_instance_accessor(method, :restore_called)
|
130
|
+
replace_instance_method(method, :restore_original_method) { self.restore_called = true }
|
130
131
|
|
131
132
|
method.unstub
|
132
133
|
|
@@ -134,12 +135,12 @@ class AnyInstanceMethodTest < Mocha::TestCase
|
|
134
135
|
end
|
135
136
|
|
136
137
|
def test_should_call_mock_unstub
|
137
|
-
klass =
|
138
|
+
klass = class_with_method(:method_x)
|
138
139
|
|
139
140
|
method = AnyInstanceMethod.new(klass, :method_x)
|
140
141
|
|
141
|
-
|
142
|
-
|
142
|
+
replace_instance_method(method, :remove_new_method) {}
|
143
|
+
replace_instance_method(method, :restore_original_method) {}
|
143
144
|
mocha = Class.new do
|
144
145
|
class << self
|
145
146
|
attr_accessor :unstub_method
|
@@ -148,8 +149,8 @@ class AnyInstanceMethodTest < Mocha::TestCase
|
|
148
149
|
self.unstub_method = method
|
149
150
|
end
|
150
151
|
end
|
151
|
-
|
152
|
-
|
152
|
+
define_instance_method(mocha, :any_expectations?) { true }
|
153
|
+
replace_instance_method(method, :mock) { mocha }
|
153
154
|
|
154
155
|
method.unstub
|
155
156
|
|
@@ -159,9 +160,9 @@ class AnyInstanceMethodTest < Mocha::TestCase
|
|
159
160
|
def test_should_return_any_instance_mocha_for_stubbee
|
160
161
|
mocha = Object.new
|
161
162
|
any_instance = Object.new
|
162
|
-
|
163
|
-
stubbee =
|
164
|
-
|
163
|
+
define_instance_method(any_instance, :mocha) { mocha }
|
164
|
+
stubbee = class_with_method(:method_x)
|
165
|
+
define_instance_method(stubbee, :any_instance) { any_instance }
|
165
166
|
method = AnyInstanceMethod.new(stubbee, :method_name)
|
166
167
|
assert_equal stubbee.any_instance.mocha, method.mock
|
167
168
|
end
|
@@ -1,43 +1,61 @@
|
|
1
1
|
require File.expand_path('../../test_helper', __FILE__)
|
2
2
|
require 'mocha/cardinality'
|
3
|
+
require 'mocha/invocation'
|
4
|
+
require 'mocha/return_values'
|
5
|
+
require 'mocha/yield_parameters'
|
3
6
|
|
4
7
|
class CardinalityTest < Mocha::TestCase
|
5
8
|
include Mocha
|
6
9
|
|
10
|
+
def new_invocation
|
11
|
+
Invocation.new(:irrelevant, :irrelevant)
|
12
|
+
end
|
13
|
+
|
7
14
|
def test_should_allow_invocations_if_invocation_count_has_not_yet_reached_maximum
|
8
15
|
cardinality = Cardinality.new(2, 3)
|
9
|
-
assert cardinality.invocations_allowed?
|
10
|
-
|
11
|
-
assert cardinality.invocations_allowed?
|
12
|
-
|
16
|
+
assert cardinality.invocations_allowed?
|
17
|
+
cardinality << new_invocation
|
18
|
+
assert cardinality.invocations_allowed?
|
19
|
+
cardinality << new_invocation
|
20
|
+
assert cardinality.invocations_allowed?
|
21
|
+
cardinality << new_invocation
|
22
|
+
assert !cardinality.invocations_allowed?
|
13
23
|
end
|
14
24
|
|
15
25
|
def test_should_be_satisfied_if_invocations_so_far_have_reached_required_threshold
|
16
26
|
cardinality = Cardinality.new(2, 3)
|
17
|
-
assert !cardinality.satisfied?
|
18
|
-
|
19
|
-
assert cardinality.satisfied?
|
20
|
-
|
27
|
+
assert !cardinality.satisfied?
|
28
|
+
cardinality << new_invocation
|
29
|
+
assert !cardinality.satisfied?
|
30
|
+
cardinality << new_invocation
|
31
|
+
assert cardinality.satisfied?
|
32
|
+
cardinality << new_invocation
|
33
|
+
assert cardinality.satisfied?
|
21
34
|
end
|
22
35
|
|
23
|
-
def
|
24
|
-
assert_equal 'allowed any number of times', Cardinality.at_least(0).
|
25
|
-
|
26
|
-
assert_equal 'expected at
|
27
|
-
assert_equal 'expected at
|
28
|
-
|
36
|
+
def test_should_describe_cardinality_defined_using_at_least
|
37
|
+
assert_equal 'allowed any number of times', Cardinality.at_least(0).anticipated_times
|
38
|
+
assert_equal 'expected at least once', Cardinality.at_least(1).anticipated_times
|
39
|
+
assert_equal 'expected at least twice', Cardinality.at_least(2).anticipated_times
|
40
|
+
assert_equal 'expected at least 3 times', Cardinality.at_least(3).anticipated_times
|
41
|
+
end
|
29
42
|
|
30
|
-
|
31
|
-
assert_equal 'expected at
|
32
|
-
assert_equal 'expected at
|
43
|
+
def test_should_describe_cardinality_defined_using_at_most
|
44
|
+
assert_equal 'expected at most once', Cardinality.at_most(1).anticipated_times
|
45
|
+
assert_equal 'expected at most twice', Cardinality.at_most(2).anticipated_times
|
46
|
+
assert_equal 'expected at most 3 times', Cardinality.at_most(3).anticipated_times
|
47
|
+
end
|
33
48
|
|
34
|
-
|
35
|
-
assert_equal 'expected
|
36
|
-
assert_equal 'expected exactly
|
37
|
-
assert_equal 'expected exactly
|
49
|
+
def test_should_describe_cardinality_defined_using_exactly
|
50
|
+
assert_equal 'expected never', Cardinality.exactly(0).anticipated_times
|
51
|
+
assert_equal 'expected exactly once', Cardinality.exactly(1).anticipated_times
|
52
|
+
assert_equal 'expected exactly twice', Cardinality.exactly(2).anticipated_times
|
53
|
+
assert_equal 'expected exactly 3 times', Cardinality.exactly(3).anticipated_times
|
54
|
+
end
|
38
55
|
|
39
|
-
|
40
|
-
assert_equal 'expected between
|
56
|
+
def test_should_describe_cardinality_defined_using_times_with_range
|
57
|
+
assert_equal 'expected between 2 and 4 times', Cardinality.times(2..4).anticipated_times
|
58
|
+
assert_equal 'expected between 1 and 3 times', Cardinality.times(1..3).anticipated_times
|
41
59
|
end
|
42
60
|
|
43
61
|
def test_should_need_verifying
|
data/test/unit/central_test.rb
CHANGED
@@ -51,7 +51,7 @@ class ClassMethodsTest < Mocha::TestCase
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def test_should_use_stubba_class_method_for_class
|
54
|
-
assert_equal Mocha::
|
54
|
+
assert_equal Mocha::InstanceMethod, @klass.stubba_method
|
55
55
|
end
|
56
56
|
|
57
57
|
def test_should_use_stubba_class_method_for_any_instance
|
@@ -3,35 +3,35 @@ require 'mocha/configuration'
|
|
3
3
|
|
4
4
|
class ConfigurationTest < Mocha::TestCase
|
5
5
|
def test_allow_temporarily_changes_config_when_given_block
|
6
|
-
Mocha
|
6
|
+
Mocha.configure { |c| c.stubbing_method_unnecessarily = :warn }
|
7
7
|
yielded = false
|
8
|
-
Mocha::Configuration.
|
8
|
+
Mocha::Configuration.override(:stubbing_method_unnecessarily => :allow) do
|
9
9
|
yielded = true
|
10
|
-
|
10
|
+
assert_equal :allow, Mocha.configuration.stubbing_method_unnecessarily
|
11
11
|
end
|
12
12
|
assert yielded
|
13
|
-
|
13
|
+
assert_equal :warn, Mocha.configuration.stubbing_method_unnecessarily
|
14
14
|
end
|
15
15
|
|
16
16
|
def test_prevent_temporarily_changes_config_when_given_block
|
17
|
-
Mocha
|
17
|
+
Mocha.configure { |c| c.stubbing_method_unnecessarily = :allow }
|
18
18
|
yielded = false
|
19
|
-
Mocha::Configuration.
|
19
|
+
Mocha::Configuration.override(:stubbing_method_unnecessarily => :prevent) do
|
20
20
|
yielded = true
|
21
|
-
|
21
|
+
assert_equal :prevent, Mocha.configuration.stubbing_method_unnecessarily
|
22
22
|
end
|
23
23
|
assert yielded
|
24
|
-
|
24
|
+
assert_equal :allow, Mocha.configuration.stubbing_method_unnecessarily
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_warn_when_temporarily_changes_config_when_given_block
|
28
|
-
Mocha
|
28
|
+
Mocha.configure { |c| c.stubbing_method_unnecessarily = :allow }
|
29
29
|
yielded = false
|
30
|
-
Mocha::Configuration.
|
30
|
+
Mocha::Configuration.override(:stubbing_method_unnecessarily => :warn) do
|
31
31
|
yielded = true
|
32
|
-
|
32
|
+
assert_equal :warn, Mocha.configuration.stubbing_method_unnecessarily
|
33
33
|
end
|
34
34
|
assert yielded
|
35
|
-
|
35
|
+
assert_equal :allow, Mocha.configuration.stubbing_method_unnecessarily
|
36
36
|
end
|
37
37
|
end
|
@@ -1,40 +1,45 @@
|
|
1
1
|
require File.expand_path('../../test_helper', __FILE__)
|
2
2
|
|
3
|
+
require 'mocha/invocation'
|
3
4
|
require 'mocha/exception_raiser'
|
4
5
|
require 'timeout'
|
5
6
|
|
6
7
|
class ExceptionRaiserTest < Mocha::TestCase
|
7
8
|
include Mocha
|
8
9
|
|
10
|
+
def new_invocation
|
11
|
+
Invocation.new(:irrelevant, :irrelevant)
|
12
|
+
end
|
13
|
+
|
9
14
|
def test_should_raise_exception_with_specified_class_and_default_message
|
10
15
|
exception_class = Class.new(StandardError)
|
11
16
|
raiser = ExceptionRaiser.new(exception_class, nil)
|
12
|
-
exception = assert_raises(exception_class) { raiser.evaluate }
|
17
|
+
exception = assert_raises(exception_class) { raiser.evaluate(new_invocation) }
|
13
18
|
assert_equal exception_class.to_s, exception.message
|
14
19
|
end
|
15
20
|
|
16
21
|
def test_should_raise_exception_with_specified_class_and_message
|
17
22
|
exception_class = Class.new(StandardError)
|
18
23
|
raiser = ExceptionRaiser.new(exception_class, 'message')
|
19
|
-
exception = assert_raises(exception_class) { raiser.evaluate }
|
24
|
+
exception = assert_raises(exception_class) { raiser.evaluate(new_invocation) }
|
20
25
|
assert_equal 'message', exception.message
|
21
26
|
end
|
22
27
|
|
23
28
|
def test_should_raise_exception_instance
|
24
29
|
exception_class = Class.new(StandardError)
|
25
30
|
raiser = ExceptionRaiser.new(exception_class.new('message'), nil)
|
26
|
-
exception = assert_raises(exception_class) { raiser.evaluate }
|
31
|
+
exception = assert_raises(exception_class) { raiser.evaluate(new_invocation) }
|
27
32
|
assert_equal 'message', exception.message
|
28
33
|
end
|
29
34
|
|
30
35
|
def test_should_raise_interrupt_exception_with_default_message_so_it_works_in_ruby_1_8_6
|
31
36
|
raiser = ExceptionRaiser.new(Interrupt, nil)
|
32
|
-
assert_raises(Interrupt) { raiser.evaluate }
|
37
|
+
assert_raises(Interrupt) { raiser.evaluate(new_invocation) }
|
33
38
|
end
|
34
39
|
|
35
40
|
def test_should_raise_subclass_of_interrupt_exception_with_default_message_so_it_works_in_ruby_1_8_6
|
36
41
|
exception_class = Class.new(Interrupt)
|
37
42
|
raiser = ExceptionRaiser.new(exception_class, nil)
|
38
|
-
assert_raises(exception_class) { raiser.evaluate }
|
43
|
+
assert_raises(exception_class) { raiser.evaluate(new_invocation) }
|
39
44
|
end
|
40
45
|
end
|