mocha 1.4.0 → 1.7.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 +5 -5
- data/.rubocop.yml +58 -0
- data/.rubocop_todo.yml +50 -0
- data/README.md +12 -71
- data/RELEASE.md +19 -0
- data/Rakefile +35 -28
- data/bin/build-matrix +17 -18
- data/lib/mocha/any_instance_method.rb +19 -25
- data/lib/mocha/api.rb +1 -5
- data/lib/mocha/argument_iterator.rb +4 -8
- data/lib/mocha/backtrace_filter.rb +1 -5
- data/lib/mocha/cardinality.rb +20 -27
- data/lib/mocha/central.rb +21 -12
- data/lib/mocha/change_state_side_effect.rb +0 -4
- data/lib/mocha/class_method.rb +31 -37
- data/lib/mocha/class_methods.rb +11 -15
- data/lib/mocha/configuration.rb +69 -10
- data/lib/mocha/debug.rb +3 -2
- data/lib/mocha/deprecation.rb +4 -11
- data/lib/mocha/detection/mini_test.rb +0 -2
- data/lib/mocha/detection/test_unit.rb +4 -4
- data/lib/mocha/error_with_filtered_backtrace.rb +13 -0
- data/lib/mocha/exception_raiser.rb +2 -5
- data/lib/mocha/expectation.rb +15 -18
- data/lib/mocha/expectation_error.rb +2 -0
- data/lib/mocha/expectation_error_factory.rb +0 -1
- data/lib/mocha/expectation_list.rb +2 -6
- data/lib/mocha/hooks.rb +1 -3
- data/lib/mocha/in_state_ordering_constraint.rb +0 -4
- data/lib/mocha/inspect.rb +3 -5
- data/lib/mocha/instance_method.rb +0 -2
- data/lib/mocha/integration/mini_test/adapter.rb +2 -4
- data/lib/mocha/integration/mini_test/exception_translation.rb +1 -1
- data/lib/mocha/integration/mini_test/nothing.rb +4 -4
- data/lib/mocha/integration/mini_test/version_13.rb +4 -1
- data/lib/mocha/integration/mini_test/version_140.rb +4 -1
- data/lib/mocha/integration/mini_test/version_141.rb +4 -1
- data/lib/mocha/integration/mini_test/version_142_to_172.rb +4 -1
- data/lib/mocha/integration/mini_test/version_200.rb +4 -1
- data/lib/mocha/integration/mini_test/version_201_to_222.rb +4 -1
- data/lib/mocha/integration/mini_test/version_2110_to_2111.rb +4 -1
- data/lib/mocha/integration/mini_test/version_2112_to_320.rb +4 -1
- data/lib/mocha/integration/mini_test/version_230_to_2101.rb +4 -1
- data/lib/mocha/integration/monkey_patcher.rb +5 -7
- data/lib/mocha/integration/test_unit/adapter.rb +5 -6
- data/lib/mocha/integration/test_unit/gem_version_200.rb +5 -2
- data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +5 -2
- data/lib/mocha/integration/test_unit/gem_version_203_to_220.rb +5 -2
- data/lib/mocha/integration/test_unit/gem_version_230_to_250.rb +5 -2
- data/lib/mocha/integration/test_unit/nothing.rb +4 -4
- data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +4 -1
- data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +4 -1
- data/lib/mocha/integration.rb +3 -4
- data/lib/mocha/is_a.rb +0 -2
- data/lib/mocha/logger.rb +0 -4
- data/lib/mocha/method_matcher.rb +1 -5
- data/lib/mocha/minitest.rb +1 -1
- data/lib/mocha/mock.rb +25 -30
- data/lib/mocha/mockery.rb +62 -43
- data/lib/mocha/module_method.rb +0 -10
- data/lib/mocha/module_methods.rb +0 -4
- data/lib/mocha/multiple_yields.rb +0 -5
- data/lib/mocha/names.rb +1 -11
- data/lib/mocha/no_yields.rb +1 -7
- data/lib/mocha/not_initialized_error.rb +7 -0
- data/lib/mocha/object_methods.rb +16 -16
- data/lib/mocha/parameter_matchers/all_of.rb +1 -7
- data/lib/mocha/parameter_matchers/any_of.rb +1 -7
- data/lib/mocha/parameter_matchers/any_parameters.rb +3 -9
- data/lib/mocha/parameter_matchers/anything.rb +2 -8
- data/lib/mocha/parameter_matchers/base.rb +6 -12
- data/lib/mocha/parameter_matchers/equals.rb +0 -6
- data/lib/mocha/parameter_matchers/equivalent_uri.rb +6 -6
- data/lib/mocha/parameter_matchers/has_entries.rb +2 -6
- data/lib/mocha/parameter_matchers/has_entry.rb +8 -11
- data/lib/mocha/parameter_matchers/has_key.rb +2 -6
- data/lib/mocha/parameter_matchers/has_value.rb +2 -6
- data/lib/mocha/parameter_matchers/includes.rb +2 -6
- data/lib/mocha/parameter_matchers/instance_of.rb +0 -6
- data/lib/mocha/parameter_matchers/is_a.rb +2 -6
- data/lib/mocha/parameter_matchers/kind_of.rb +2 -6
- data/lib/mocha/parameter_matchers/not.rb +2 -6
- data/lib/mocha/parameter_matchers/object.rb +0 -2
- data/lib/mocha/parameter_matchers/optionally.rb +4 -10
- data/lib/mocha/parameter_matchers/regexp_matches.rb +0 -6
- data/lib/mocha/parameter_matchers/responds_with.rb +3 -8
- data/lib/mocha/parameter_matchers/yaml_equivalent.rb +2 -6
- data/lib/mocha/parameter_matchers.rb +0 -2
- data/lib/mocha/parameters_matcher.rb +6 -9
- data/lib/mocha/pretty_parameters.rb +0 -4
- data/lib/mocha/receivers.rb +10 -14
- data/lib/mocha/return_values.rb +3 -7
- data/lib/mocha/sequence.rb +4 -9
- data/lib/mocha/single_return_value.rb +0 -4
- data/lib/mocha/single_yield.rb +0 -5
- data/lib/mocha/state_machine.rb +6 -10
- data/lib/mocha/stubbing_error.rb +2 -13
- data/lib/mocha/test_unit.rb +1 -1
- data/lib/mocha/thrower.rb +2 -5
- data/lib/mocha/unexpected_invocation.rb +3 -5
- data/lib/mocha/version.rb +1 -1
- data/lib/mocha/yield_parameters.rb +3 -7
- data/mocha.gemspec +34 -58
- data/test/acceptance/acceptance_test_helper.rb +0 -6
- data/test/acceptance/bug_18914_test.rb +7 -12
- data/test/acceptance/bug_21465_test.rb +0 -2
- data/test/acceptance/bug_21563_test.rb +0 -2
- data/test/acceptance/exception_rescue_test.rb +7 -8
- data/test/acceptance/expectations_on_multiple_methods_test.rb +2 -1
- data/test/acceptance/expected_invocation_count_test.rb +27 -29
- data/test/acceptance/failure_messages_test.rb +3 -5
- data/test/acceptance/issue_272_test.rb +0 -1
- data/test/acceptance/issue_65_test.rb +15 -13
- data/test/acceptance/issue_70_test.rb +0 -1
- data/test/acceptance/mocha_example_test.rb +5 -6
- data/test/acceptance/mocha_test_result_test.rb +7 -7
- data/test/acceptance/mock_test.rb +2 -4
- data/test/acceptance/mock_with_initializer_block_test.rb +0 -2
- data/test/acceptance/mocked_methods_dispatch_test.rb +0 -2
- data/test/acceptance/multiple_expectations_failure_message_test.rb +16 -17
- data/test/acceptance/optional_parameters_test.rb +0 -2
- data/test/acceptance/parameter_matcher_test.rb +2 -3
- data/test/acceptance/partial_mocks_test.rb +4 -6
- data/test/acceptance/prepend_test.rb +14 -16
- data/test/acceptance/prevent_use_of_mocha_outside_test_test.rb +77 -0
- data/test/acceptance/raise_exception_test.rb +2 -4
- data/test/acceptance/return_value_test.rb +0 -2
- data/test/acceptance/sequence_test.rb +9 -11
- data/test/acceptance/states_test.rb +6 -4
- data/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb +33 -2
- data/test/acceptance/stub_any_instance_method_test.rb +19 -17
- data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +8 -8
- data/test/acceptance/stub_class_method_defined_on_class_test.rb +2 -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 +34 -1
- data/test/acceptance/stub_everything_test.rb +1 -3
- data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +6 -7
- 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 +63 -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 +2 -1
- data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -2
- data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -1
- data/test/acceptance/stub_module_method_test.rb +13 -13
- data/test/acceptance/stub_test.rb +1 -3
- data/test/acceptance/stubba_example_test.rb +6 -14
- data/test/acceptance/stubba_test_result_test.rb +5 -8
- data/test/acceptance/stubbing_error_backtrace_test.rb +2 -2
- data/test/acceptance/stubbing_frozen_object_test.rb +2 -1
- data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +12 -7
- data/test/acceptance/stubbing_method_unnecessarily_test.rb +0 -2
- data/test/acceptance/stubbing_nil_test.rb +4 -5
- data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +1 -3
- data/test/acceptance/stubbing_non_existent_class_method_test.rb +3 -3
- data/test/acceptance/stubbing_non_existent_instance_method_test.rb +1 -3
- data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +0 -2
- data/test/acceptance/stubbing_non_public_class_method_test.rb +3 -3
- data/test/acceptance/stubbing_non_public_instance_method_test.rb +1 -3
- data/test/acceptance/stubbing_on_non_mock_object_test.rb +4 -10
- data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +3 -4
- data/test/acceptance/throw_test.rb +0 -2
- data/test/acceptance/unexpected_invocation_test.rb +2 -3
- data/test/acceptance/unstubbing_test.rb +41 -14
- data/test/deprecation_disabler.rb +0 -1
- data/test/execution_point.rb +2 -4
- data/test/integration/mini_test_test.rb +2 -2
- data/test/integration/shared_tests.rb +24 -22
- data/test/integration/test_unit_test.rb +2 -2
- data/test/method_definer.rb +1 -3
- data/test/mini_test_result.rb +17 -11
- data/test/minitest_result.rb +0 -1
- data/test/simple_counter.rb +0 -2
- data/test/test_helper.rb +13 -5
- data/test/test_runner.rb +2 -4
- data/test/test_unit_result.rb +4 -2
- data/test/unit/any_instance_method_test.rb +31 -17
- data/test/unit/array_inspect_test.rb +2 -4
- data/test/unit/backtrace_filter_test.rb +3 -5
- data/test/unit/cardinality_test.rb +0 -2
- data/test/unit/central_test.rb +26 -27
- data/test/unit/change_state_side_effect_test.rb +0 -4
- data/test/unit/class_method_test.rb +59 -34
- data/test/unit/class_methods_test.rb +34 -5
- data/test/unit/configuration_test.rb +1 -2
- data/test/unit/date_time_inspect_test.rb +1 -3
- data/test/unit/exception_raiser_test.rb +0 -2
- data/test/unit/expectation_list_test.rb +0 -2
- data/test/unit/expectation_test.rb +41 -46
- data/test/unit/hash_inspect_test.rb +3 -5
- data/test/unit/hooks_test.rb +14 -8
- data/test/unit/in_state_ordering_constraint_test.rb +0 -4
- data/test/unit/method_matcher_test.rb +1 -3
- data/test/unit/mock_test.rb +35 -22
- data/test/unit/mockery_test.rb +45 -28
- data/test/unit/module_methods_test.rb +0 -2
- data/test/unit/multiple_yields_test.rb +0 -2
- data/test/unit/no_yields_test.rb +0 -2
- data/test/unit/object_inspect_test.rb +16 -7
- data/test/unit/object_methods_test.rb +22 -6
- data/test/unit/parameter_matchers/all_of_test.rb +0 -2
- data/test/unit/parameter_matchers/any_of_test.rb +0 -2
- data/test/unit/parameter_matchers/anything_test.rb +2 -4
- data/test/unit/parameter_matchers/equals_test.rb +1 -3
- data/test/unit/parameter_matchers/equivalent_uri_test.rb +0 -1
- data/test/unit/parameter_matchers/has_entries_test.rb +2 -2
- data/test/unit/parameter_matchers/has_entry_test.rb +13 -14
- data/test/unit/parameter_matchers/has_key_test.rb +0 -1
- data/test/unit/parameter_matchers/has_value_test.rb +0 -2
- data/test/unit/parameter_matchers/includes_test.rb +8 -9
- data/test/unit/parameter_matchers/instance_of_test.rb +1 -3
- data/test/unit/parameter_matchers/is_a_test.rb +1 -3
- data/test/unit/parameter_matchers/kind_of_test.rb +1 -3
- data/test/unit/parameter_matchers/not_test.rb +0 -2
- data/test/unit/parameter_matchers/regexp_matches_test.rb +1 -2
- data/test/unit/parameter_matchers/responds_with_test.rb +9 -3
- data/test/unit/parameter_matchers/stub_matcher.rb +0 -4
- data/test/unit/parameter_matchers/yaml_equivalent_test.rb +1 -3
- data/test/unit/parameters_matcher_test.rb +2 -4
- data/test/unit/receivers_test.rb +35 -5
- data/test/unit/return_values_test.rb +3 -5
- data/test/unit/sequence_test.rb +1 -5
- data/test/unit/single_return_value_test.rb +0 -2
- data/test/unit/single_yield_test.rb +0 -2
- data/test/unit/state_machine_test.rb +1 -3
- data/test/unit/string_inspect_test.rb +2 -4
- data/test/unit/thrower_test.rb +0 -2
- data/test/unit/yield_parameters_test.rb +0 -2
- data/yard-templates/default/layout/html/setup.rb +2 -3
- metadata +25 -6
@@ -2,7 +2,6 @@ require File.expand_path('../acceptance_test_helper', __FILE__)
|
|
2
2
|
require 'mocha/setup'
|
3
3
|
|
4
4
|
class ParameterMatcherTest < Mocha::TestCase
|
5
|
-
|
6
5
|
include AcceptanceTest
|
7
6
|
|
8
7
|
def setup
|
@@ -264,7 +263,7 @@ class ParameterMatcherTest < Mocha::TestCase
|
|
264
263
|
def test_should_match_parameter_when_value_is_divisible_by_four
|
265
264
|
test_result = run_as_test do
|
266
265
|
mock = mock()
|
267
|
-
mock.expects(:method).with { |actual_value| actual_value % 4
|
266
|
+
mock.expects(:method).with { |actual_value| (actual_value % 4).zero? }
|
268
267
|
mock.method(8)
|
269
268
|
end
|
270
269
|
assert_passed(test_result)
|
@@ -273,7 +272,7 @@ class ParameterMatcherTest < Mocha::TestCase
|
|
273
272
|
def test_should_not_match_parameter_when_value_is_not_divisible_by_four
|
274
273
|
test_result = run_as_test do
|
275
274
|
mock = mock()
|
276
|
-
mock.expects(:method).with { |actual_value| actual_value % 4
|
275
|
+
mock.expects(:method).with { |actual_value| (actual_value % 4).zero? }
|
277
276
|
mock.method(9)
|
278
277
|
end
|
279
278
|
assert_failed(test_result)
|
@@ -2,7 +2,6 @@ require File.expand_path('../acceptance_test_helper', __FILE__)
|
|
2
2
|
require 'mocha/setup'
|
3
3
|
|
4
4
|
class PartialMockTest < Mocha::TestCase
|
5
|
-
|
6
5
|
include AcceptanceTest
|
7
6
|
|
8
7
|
def setup
|
@@ -15,8 +14,8 @@ class PartialMockTest < Mocha::TestCase
|
|
15
14
|
|
16
15
|
def test_should_pass_if_all_expectations_are_satisfied
|
17
16
|
test_result = run_as_test do
|
18
|
-
partial_mock_one =
|
19
|
-
partial_mock_two =
|
17
|
+
partial_mock_one = 'partial_mock_one'
|
18
|
+
partial_mock_two = 'partial_mock_two'
|
20
19
|
|
21
20
|
partial_mock_one.expects(:first)
|
22
21
|
partial_mock_one.expects(:second)
|
@@ -31,8 +30,8 @@ class PartialMockTest < Mocha::TestCase
|
|
31
30
|
|
32
31
|
def test_should_fail_if_all_expectations_are_not_satisfied
|
33
32
|
test_result = run_as_test do
|
34
|
-
partial_mock_one =
|
35
|
-
partial_mock_two =
|
33
|
+
partial_mock_one = 'partial_mock_one'
|
34
|
+
partial_mock_two = 'partial_mock_two'
|
36
35
|
|
37
36
|
partial_mock_one.expects(:first)
|
38
37
|
partial_mock_one.expects(:second)
|
@@ -43,5 +42,4 @@ class PartialMockTest < Mocha::TestCase
|
|
43
42
|
end
|
44
43
|
assert_failed(test_result)
|
45
44
|
end
|
46
|
-
|
47
45
|
end
|
@@ -3,7 +3,6 @@ require 'mocha/setup'
|
|
3
3
|
require 'mocha/ruby_version'
|
4
4
|
|
5
5
|
class PrependTest < Mocha::TestCase
|
6
|
-
|
7
6
|
include AcceptanceTest
|
8
7
|
|
9
8
|
def setup
|
@@ -18,13 +17,13 @@ class PrependTest < Mocha::TestCase
|
|
18
17
|
|
19
18
|
module Mod1
|
20
19
|
def my_method
|
21
|
-
super +
|
20
|
+
super + ' World'
|
22
21
|
end
|
23
22
|
end
|
24
23
|
|
25
24
|
module Mod2
|
26
25
|
def my_method
|
27
|
-
super +
|
26
|
+
super + ' Wide'
|
28
27
|
end
|
29
28
|
end
|
30
29
|
|
@@ -33,7 +32,7 @@ class PrependTest < Mocha::TestCase
|
|
33
32
|
prepend Mod2
|
34
33
|
|
35
34
|
def my_method
|
36
|
-
|
35
|
+
'Hello'
|
37
36
|
end
|
38
37
|
end
|
39
38
|
|
@@ -43,7 +42,7 @@ class PrependTest < Mocha::TestCase
|
|
43
42
|
prepend Mod2
|
44
43
|
|
45
44
|
def my_method
|
46
|
-
|
45
|
+
'Hello'
|
47
46
|
end
|
48
47
|
end
|
49
48
|
end
|
@@ -51,12 +50,12 @@ class PrependTest < Mocha::TestCase
|
|
51
50
|
def test_stubbing_any_instance_with_multiple_prepended_methods
|
52
51
|
assert_snapshot_unchanged(Klass1) do
|
53
52
|
test_result = run_as_test do
|
54
|
-
Klass1.any_instance.stubs(:my_method).returns(
|
55
|
-
assert_equal
|
53
|
+
Klass1.any_instance.stubs(:my_method).returns('Bye World')
|
54
|
+
assert_equal 'Bye World', Klass1.new.my_method
|
56
55
|
end
|
57
56
|
assert_passed(test_result)
|
58
57
|
end
|
59
|
-
assert_equal
|
58
|
+
assert_equal 'Hello World Wide', Klass1.new.my_method
|
60
59
|
end
|
61
60
|
|
62
61
|
def test_stubbing_instance_with_multiple_prepended_methods
|
@@ -64,26 +63,25 @@ class PrependTest < Mocha::TestCase
|
|
64
63
|
|
65
64
|
assert_snapshot_unchanged(object) do
|
66
65
|
test_result = run_as_test do
|
67
|
-
object.stubs(:my_method).returns(
|
68
|
-
assert_equal
|
69
|
-
assert_equal
|
66
|
+
object.stubs(:my_method).returns('Bye World')
|
67
|
+
assert_equal 'Bye World', object.my_method
|
68
|
+
assert_equal 'Hello World Wide', Klass1.new.my_method
|
70
69
|
end
|
71
70
|
assert_passed(test_result)
|
72
71
|
end
|
73
|
-
assert_equal
|
72
|
+
assert_equal 'Hello World Wide', object.my_method
|
74
73
|
end
|
75
74
|
|
76
75
|
def test_stubbing_a_prepended_class_method
|
77
76
|
assert_snapshot_unchanged(Klass2) do
|
78
77
|
test_result = run_as_test do
|
79
|
-
Klass2.stubs(:my_method).returns(
|
80
|
-
assert_equal
|
78
|
+
Klass2.stubs(:my_method).returns('Bye World')
|
79
|
+
assert_equal 'Bye World', Klass2.my_method
|
81
80
|
end
|
82
81
|
assert_passed(test_result)
|
83
82
|
end
|
84
|
-
assert_equal
|
83
|
+
assert_equal 'Hello World Wide', Klass2.my_method
|
85
84
|
end
|
86
85
|
|
87
86
|
end
|
88
|
-
|
89
87
|
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require File.expand_path('../acceptance_test_helper', __FILE__)
|
2
|
+
require 'mocha/setup'
|
3
|
+
require 'mocha/not_initialized_error'
|
4
|
+
|
5
|
+
class PreventUseOfMochaOutsideTestTest < Mocha::TestCase
|
6
|
+
include AcceptanceTest
|
7
|
+
|
8
|
+
def setup
|
9
|
+
setup_acceptance_test
|
10
|
+
mocha_teardown
|
11
|
+
end
|
12
|
+
|
13
|
+
def teardown
|
14
|
+
teardown_acceptance_test
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_should_raise_exception_when_mock_called_outside_test
|
18
|
+
assert_raises(Mocha::NotInitializedError) { mock('object') }
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_should_raise_exception_when_stub_called_outside_test
|
22
|
+
assert_raises(Mocha::NotInitializedError) { stub('object') }
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_should_raise_exception_when_stub_everything_called_outside_test
|
26
|
+
assert_raises(Mocha::NotInitializedError) { stub_everything('object') }
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_should_raise_exception_when_states_called_outside_test
|
30
|
+
assert_raises(Mocha::NotInitializedError) { states('state-machine') }
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_should_raise_exception_when_expects_called_on_instance_outside_test
|
34
|
+
instance = Class.new.new
|
35
|
+
assert_raises(Mocha::NotInitializedError) { instance.expects(:expected_method) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_should_raise_exception_when_expects_called_on_class_outside_test
|
39
|
+
klass = Class.new
|
40
|
+
assert_raises(Mocha::NotInitializedError) { klass.expects(:expected_method) }
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_should_raise_exception_when_expects_called_on_any_instance_outside_test
|
44
|
+
klass = Class.new
|
45
|
+
assert_raises(Mocha::NotInitializedError) { klass.any_instance.expects(:expected_method) }
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_should_raise_exception_when_stubs_called_on_instance_outside_test
|
49
|
+
instance = Class.new.new
|
50
|
+
assert_raises(Mocha::NotInitializedError) { instance.stubs(:expected_method) }
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_should_raise_exception_when_stubs_called_on_class_outside_test
|
54
|
+
klass = Class.new
|
55
|
+
assert_raises(Mocha::NotInitializedError) { klass.stubs(:expected_method) }
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_should_raise_exception_when_stubs_called_on_any_instance_outside_test
|
59
|
+
klass = Class.new
|
60
|
+
assert_raises(Mocha::NotInitializedError) { klass.any_instance.stubs(:expected_method) }
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_should_raise_exception_when_unstub_called_on_instance_outside_test
|
64
|
+
instance = Class.new.new
|
65
|
+
assert_raises(Mocha::NotInitializedError) { instance.unstub(:expected_method) }
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_should_raise_exception_when_unstub_called_on_class_outside_test
|
69
|
+
klass = Class.new
|
70
|
+
assert_raises(Mocha::NotInitializedError) { klass.unstub(:expected_method) }
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_should_raise_exception_when_unstub_called_on_any_instance_outside_test
|
74
|
+
klass = Class.new
|
75
|
+
assert_raises(Mocha::NotInitializedError) { klass.any_instance.unstub(:expected_method) }
|
76
|
+
end
|
77
|
+
end
|
@@ -2,7 +2,6 @@ require File.expand_path('../acceptance_test_helper', __FILE__)
|
|
2
2
|
require 'mocha/setup'
|
3
3
|
|
4
4
|
class RaiseExceptionTest < Mocha::TestCase
|
5
|
-
|
6
5
|
include AcceptanceTest
|
7
6
|
|
8
7
|
def setup
|
@@ -17,9 +16,9 @@ class RaiseExceptionTest < Mocha::TestCase
|
|
17
16
|
exception_class = Class.new(StandardError)
|
18
17
|
test_result = run_as_test do
|
19
18
|
foo = stub('foo')
|
20
|
-
foo.stubs(:bar).raises(exception_class,
|
19
|
+
foo.stubs(:bar).raises(exception_class, 'my-message')
|
21
20
|
exception = assert_raises(exception_class) { foo.bar }
|
22
|
-
assert_equal
|
21
|
+
assert_equal 'my-message', exception.message
|
23
22
|
end
|
24
23
|
assert_passed(test_result)
|
25
24
|
end
|
@@ -35,5 +34,4 @@ class RaiseExceptionTest < Mocha::TestCase
|
|
35
34
|
end
|
36
35
|
assert_passed(test_result)
|
37
36
|
end
|
38
|
-
|
39
37
|
end
|
@@ -2,7 +2,6 @@ require File.expand_path('../acceptance_test_helper', __FILE__)
|
|
2
2
|
require 'mocha/setup'
|
3
3
|
|
4
4
|
class ReturnValueTest < Mocha::TestCase
|
5
|
-
|
6
5
|
include AcceptanceTest
|
7
6
|
|
8
7
|
def setup
|
@@ -48,5 +47,4 @@ class ReturnValueTest < Mocha::TestCase
|
|
48
47
|
end
|
49
48
|
assert_passed(test_result)
|
50
49
|
end
|
51
|
-
|
52
50
|
end
|
@@ -2,7 +2,6 @@ require File.expand_path('../acceptance_test_helper', __FILE__)
|
|
2
2
|
require 'mocha/setup'
|
3
3
|
|
4
4
|
class SequenceTest < Mocha::TestCase
|
5
|
-
|
6
5
|
include AcceptanceTest
|
7
6
|
|
8
7
|
def setup
|
@@ -73,8 +72,8 @@ class SequenceTest < Mocha::TestCase
|
|
73
72
|
|
74
73
|
def test_should_constrain_invocations_to_occur_in_expected_order_even_if_expected_on_partial_mocks
|
75
74
|
test_result = run_as_test do
|
76
|
-
partial_mock_one =
|
77
|
-
partial_mock_two =
|
75
|
+
partial_mock_one = '1'
|
76
|
+
partial_mock_two = '2'
|
78
77
|
sequence = sequence('one')
|
79
78
|
|
80
79
|
partial_mock_one.expects(:first).in_sequence(sequence)
|
@@ -88,8 +87,8 @@ class SequenceTest < Mocha::TestCase
|
|
88
87
|
|
89
88
|
def test_should_allow_invocations_in_sequence_even_if_expected_on_partial_mocks
|
90
89
|
test_result = run_as_test do
|
91
|
-
partial_mock_one =
|
92
|
-
partial_mock_two =
|
90
|
+
partial_mock_one = '1'
|
91
|
+
partial_mock_two = '2'
|
93
92
|
sequence = sequence('one')
|
94
93
|
|
95
94
|
partial_mock_one.expects(:first).in_sequence(sequence)
|
@@ -148,7 +147,7 @@ class SequenceTest < Mocha::TestCase
|
|
148
147
|
mock.first
|
149
148
|
end
|
150
149
|
assert_failed(test_result)
|
151
|
-
assert_match Regexp.new(%
|
150
|
+
assert_match Regexp.new(%(in sequence "one")), test_result.failures.first.message
|
152
151
|
end
|
153
152
|
|
154
153
|
def test_should_allow_expectations_to_be_in_more_than_one_sequence
|
@@ -166,8 +165,8 @@ class SequenceTest < Mocha::TestCase
|
|
166
165
|
mock.second
|
167
166
|
end
|
168
167
|
assert_failed(test_result)
|
169
|
-
assert_match Regexp.new(%
|
170
|
-
assert_match Regexp.new(%
|
168
|
+
assert_match Regexp.new(%(in sequence "one")), test_result.failures.first.message
|
169
|
+
assert_match Regexp.new(%(in sequence "two")), test_result.failures.first.message
|
171
170
|
end
|
172
171
|
|
173
172
|
def test_should_have_shortcut_for_expectations_to_be_in_more_than_one_sequence
|
@@ -185,8 +184,7 @@ class SequenceTest < Mocha::TestCase
|
|
185
184
|
mock.second
|
186
185
|
end
|
187
186
|
assert_failed(test_result)
|
188
|
-
assert_match Regexp.new(%
|
189
|
-
assert_match Regexp.new(%
|
187
|
+
assert_match Regexp.new(%(in sequence "one")), test_result.failures.first.message
|
188
|
+
assert_match Regexp.new(%(in sequence "two")), test_result.failures.first.message
|
190
189
|
end
|
191
|
-
|
192
190
|
end
|
@@ -2,7 +2,6 @@ require File.expand_path('../acceptance_test_helper', __FILE__)
|
|
2
2
|
require 'mocha/setup'
|
3
3
|
|
4
4
|
class StatesTest < Mocha::TestCase
|
5
|
-
|
6
5
|
include AcceptanceTest
|
7
6
|
|
8
7
|
def setup
|
@@ -58,13 +57,16 @@ class StatesTest < Mocha::TestCase
|
|
58
57
|
mock = mock()
|
59
58
|
readiness = states('readiness')
|
60
59
|
|
61
|
-
mock.expects(:first).raises
|
60
|
+
mock.expects(:first).raises.then(readiness.is('ready'))
|
62
61
|
mock.expects(:second).when(readiness.is('ready'))
|
63
62
|
|
64
|
-
|
63
|
+
begin
|
64
|
+
mock.first
|
65
|
+
rescue StandardError
|
66
|
+
nil
|
67
|
+
end
|
65
68
|
mock.second
|
66
69
|
end
|
67
70
|
assert_passed(test_result)
|
68
71
|
end
|
69
|
-
|
70
72
|
end
|
@@ -2,7 +2,6 @@ require File.expand_path('../acceptance_test_helper', __FILE__)
|
|
2
2
|
require 'mocha/setup'
|
3
3
|
|
4
4
|
class StubAnyInstanceMethodDefinedOnSuperclassTest < Mocha::TestCase
|
5
|
-
|
6
5
|
include AcceptanceTest
|
7
6
|
|
8
7
|
def setup
|
@@ -31,4 +30,36 @@ class StubAnyInstanceMethodDefinedOnSuperclassTest < Mocha::TestCase
|
|
31
30
|
end
|
32
31
|
assert_equal :original_return_value, instance.my_superclass_method
|
33
32
|
end
|
34
|
-
|
33
|
+
|
34
|
+
def test_expect_method_on_any_instance_of_superclass_even_if_preceded_by_test_expecting_method_on_any_instance_of_subclass
|
35
|
+
superklass = Class.new do
|
36
|
+
def self.inspect
|
37
|
+
'superklass'
|
38
|
+
end
|
39
|
+
|
40
|
+
def my_instance_method; end
|
41
|
+
end
|
42
|
+
klass = Class.new(superklass) do
|
43
|
+
def self.inspect
|
44
|
+
'klass'
|
45
|
+
end
|
46
|
+
|
47
|
+
def my_instance_method; end
|
48
|
+
end
|
49
|
+
test_result = run_as_tests(
|
50
|
+
:test_1 => lambda {
|
51
|
+
klass.any_instance.expects(:my_instance_method)
|
52
|
+
klass.new.my_instance_method
|
53
|
+
},
|
54
|
+
:test_2 => lambda {
|
55
|
+
superklass.any_instance.expects(:my_instance_method)
|
56
|
+
}
|
57
|
+
)
|
58
|
+
assert_failed(test_result)
|
59
|
+
assert_equal [
|
60
|
+
'not all expectations were satisfied',
|
61
|
+
'unsatisfied expectations:',
|
62
|
+
'- expected exactly once, not yet invoked: #<AnyInstance:superklass>.my_instance_method(any_parameters)'
|
63
|
+
], test_result.failure_message_lines
|
64
|
+
end
|
65
|
+
end
|
@@ -2,7 +2,6 @@ require File.expand_path('../acceptance_test_helper', __FILE__)
|
|
2
2
|
require 'mocha/setup'
|
3
3
|
|
4
4
|
class StubAnyInstanceMethodTest < Mocha::TestCase
|
5
|
-
|
6
5
|
include AcceptanceTest
|
7
6
|
|
8
7
|
def setup
|
@@ -40,7 +39,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
40
39
|
run_as_test do
|
41
40
|
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value)
|
42
41
|
end
|
43
|
-
assert
|
42
|
+
assert(instance.public_methods(false).any? { |m| m.to_s == 'my_instance_method' })
|
44
43
|
assert_equal :original_return_value, instance.my_instance_method
|
45
44
|
end
|
46
45
|
|
@@ -51,6 +50,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
51
50
|
end
|
52
51
|
protected :my_instance_method
|
53
52
|
def self.protected(*args); end
|
53
|
+
|
54
54
|
def my_unprotected_instance_method
|
55
55
|
my_instance_method
|
56
56
|
end
|
@@ -59,7 +59,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
59
59
|
run_as_test do
|
60
60
|
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value)
|
61
61
|
end
|
62
|
-
assert
|
62
|
+
assert(instance.protected_methods(false).any? { |m| m.to_s == 'my_instance_method' })
|
63
63
|
assert_equal :original_return_value, instance.my_unprotected_instance_method
|
64
64
|
end
|
65
65
|
|
@@ -70,6 +70,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
70
70
|
end
|
71
71
|
protected :my_instance_method
|
72
72
|
def self.protected(*args); end
|
73
|
+
|
73
74
|
def my_unprotected_instance_method
|
74
75
|
my_instance_method
|
75
76
|
end
|
@@ -94,7 +95,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
94
95
|
run_as_test do
|
95
96
|
klass.any_instance.stubs(:my_instance_method).returns(:new_return_value)
|
96
97
|
end
|
97
|
-
assert
|
98
|
+
assert(instance.private_methods(false).any? { |m| m.to_s == 'my_instance_method' })
|
98
99
|
assert_equal :original_return_value, instance.send(:my_instance_method)
|
99
100
|
end
|
100
101
|
|
@@ -142,8 +143,8 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
142
143
|
assert_equal :new_return_value, instance.my_superclass_method
|
143
144
|
end
|
144
145
|
assert_passed(test_result)
|
145
|
-
assert
|
146
|
-
assert
|
146
|
+
assert(instance.public_methods(true).any? { |m| m.to_s == 'my_superclass_method' })
|
147
|
+
assert(klass.public_methods(false).none? { |m| m.to_s == 'my_superclass_method' })
|
147
148
|
assert_equal :original_return_value, instance.my_superclass_method
|
148
149
|
end
|
149
150
|
|
@@ -162,8 +163,8 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
162
163
|
assert_equal :new_return_value, instance.send(:my_superclass_method)
|
163
164
|
end
|
164
165
|
assert_passed(test_result)
|
165
|
-
assert
|
166
|
-
assert
|
166
|
+
assert(instance.protected_methods(true).any? { |m| m.to_s == 'my_superclass_method' })
|
167
|
+
assert(klass.protected_methods(false).none? { |m| m.to_s == 'my_superclass_method' })
|
167
168
|
assert_equal :original_return_value, instance.send(:my_superclass_method)
|
168
169
|
end
|
169
170
|
|
@@ -182,15 +183,16 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
182
183
|
assert_equal :new_return_value, instance.send(:my_superclass_method)
|
183
184
|
end
|
184
185
|
assert_passed(test_result)
|
185
|
-
assert
|
186
|
-
assert
|
186
|
+
assert(instance.private_methods(true).any? { |m| m.to_s == 'my_superclass_method' })
|
187
|
+
assert(klass.private_methods(false).none? { |m| m.to_s == 'my_superclass_method' })
|
187
188
|
assert_equal :original_return_value, instance.send(:my_superclass_method)
|
188
189
|
end
|
189
190
|
|
191
|
+
# rubocop:disable Lint/DuplicateMethods
|
190
192
|
def test_should_be_able_to_stub_method_if_ruby18_public_instance_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
|
191
193
|
ruby18_klass = Class.new do
|
192
194
|
class << self
|
193
|
-
def public_instance_methods(
|
195
|
+
def public_instance_methods(_include_superclass = true)
|
194
196
|
['my_instance_method']
|
195
197
|
end
|
196
198
|
end
|
@@ -205,7 +207,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
205
207
|
def test_should_be_able_to_stub_method_if_ruby19_public_instance_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
|
206
208
|
ruby19_klass = Class.new do
|
207
209
|
class << self
|
208
|
-
def public_instance_methods(
|
210
|
+
def public_instance_methods(_include_superclass = true)
|
209
211
|
[:my_instance_method]
|
210
212
|
end
|
211
213
|
end
|
@@ -220,7 +222,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
220
222
|
def test_should_be_able_to_stub_method_if_ruby18_protected_instance_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
|
221
223
|
ruby18_klass = Class.new do
|
222
224
|
class << self
|
223
|
-
def protected_instance_methods(
|
225
|
+
def protected_instance_methods(_include_superclass = true)
|
224
226
|
['my_instance_method']
|
225
227
|
end
|
226
228
|
end
|
@@ -235,7 +237,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
235
237
|
def test_should_be_able_to_stub_method_if_ruby19_protected_instance_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
|
236
238
|
ruby19_klass = Class.new do
|
237
239
|
class << self
|
238
|
-
def protected_instance_methods(
|
240
|
+
def protected_instance_methods(_include_superclass = true)
|
239
241
|
[:my_instance_method]
|
240
242
|
end
|
241
243
|
end
|
@@ -250,7 +252,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
250
252
|
def test_should_be_able_to_stub_method_if_ruby18_private_instance_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
|
251
253
|
ruby18_klass = Class.new do
|
252
254
|
class << self
|
253
|
-
def private_instance_methods(
|
255
|
+
def private_instance_methods(_include_superclass = true)
|
254
256
|
['my_instance_method']
|
255
257
|
end
|
256
258
|
end
|
@@ -265,7 +267,7 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
265
267
|
def test_should_be_able_to_stub_method_if_ruby19_private_instance_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
|
266
268
|
ruby19_klass = Class.new do
|
267
269
|
class << self
|
268
|
-
def private_instance_methods(
|
270
|
+
def private_instance_methods(_include_superclass = true)
|
269
271
|
[:my_instance_method]
|
270
272
|
end
|
271
273
|
end
|
@@ -276,5 +278,5 @@ class StubAnyInstanceMethodTest < Mocha::TestCase
|
|
276
278
|
end
|
277
279
|
assert_passed(test_result)
|
278
280
|
end
|
279
|
-
|
281
|
+
# rubocop:enable Lint/DuplicateMethods
|
280
282
|
end
|
@@ -2,7 +2,6 @@ require File.expand_path('../acceptance_test_helper', __FILE__)
|
|
2
2
|
require 'mocha/setup'
|
3
3
|
|
4
4
|
class StubClassMethodDefinedOnActiveRecordAssociationProxyTest < Mocha::TestCase
|
5
|
-
|
6
5
|
include AcceptanceTest
|
7
6
|
|
8
7
|
def setup
|
@@ -13,10 +12,11 @@ class StubClassMethodDefinedOnActiveRecordAssociationProxyTest < Mocha::TestCase
|
|
13
12
|
teardown_acceptance_test
|
14
13
|
end
|
15
14
|
|
15
|
+
# rubocop:disable Lint/DuplicateMethods
|
16
16
|
def test_should_be_able_to_stub_method_if_ruby18_public_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
|
17
17
|
ruby18_klass = Class.new do
|
18
18
|
class << self
|
19
|
-
def public_methods(
|
19
|
+
def public_methods(_include_superclass = true)
|
20
20
|
['my_class_method']
|
21
21
|
end
|
22
22
|
end
|
@@ -31,7 +31,7 @@ class StubClassMethodDefinedOnActiveRecordAssociationProxyTest < Mocha::TestCase
|
|
31
31
|
def test_should_be_able_to_stub_method_if_ruby19_public_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
|
32
32
|
ruby19_klass = Class.new do
|
33
33
|
class << self
|
34
|
-
def public_methods(
|
34
|
+
def public_methods(_include_superclass = true)
|
35
35
|
[:my_class_method]
|
36
36
|
end
|
37
37
|
end
|
@@ -46,7 +46,7 @@ class StubClassMethodDefinedOnActiveRecordAssociationProxyTest < Mocha::TestCase
|
|
46
46
|
def test_should_be_able_to_stub_method_if_ruby18_protected_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
|
47
47
|
ruby18_klass = Class.new do
|
48
48
|
class << self
|
49
|
-
def protected_methods(
|
49
|
+
def protected_methods(_include_superclass = true)
|
50
50
|
['my_class_method']
|
51
51
|
end
|
52
52
|
end
|
@@ -61,7 +61,7 @@ class StubClassMethodDefinedOnActiveRecordAssociationProxyTest < Mocha::TestCase
|
|
61
61
|
def test_should_be_able_to_stub_method_if_ruby19_protected_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
|
62
62
|
ruby19_klass = Class.new do
|
63
63
|
class << self
|
64
|
-
def protected_methods(
|
64
|
+
def protected_methods(_include_superclass = true)
|
65
65
|
[:my_class_method]
|
66
66
|
end
|
67
67
|
end
|
@@ -76,7 +76,7 @@ class StubClassMethodDefinedOnActiveRecordAssociationProxyTest < Mocha::TestCase
|
|
76
76
|
def test_should_be_able_to_stub_method_if_ruby18_private_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
|
77
77
|
ruby18_klass = Class.new do
|
78
78
|
class << self
|
79
|
-
def private_methods(
|
79
|
+
def private_methods(_include_superclass = true)
|
80
80
|
['my_class_method']
|
81
81
|
end
|
82
82
|
end
|
@@ -91,7 +91,7 @@ class StubClassMethodDefinedOnActiveRecordAssociationProxyTest < Mocha::TestCase
|
|
91
91
|
def test_should_be_able_to_stub_method_if_ruby19_private_methods_include_method_but_method_does_not_actually_exist_like_active_record_association_proxy
|
92
92
|
ruby19_klass = Class.new do
|
93
93
|
class << self
|
94
|
-
def private_methods(
|
94
|
+
def private_methods(_include_superclass = true)
|
95
95
|
[:my_class_method]
|
96
96
|
end
|
97
97
|
end
|
@@ -102,5 +102,5 @@ class StubClassMethodDefinedOnActiveRecordAssociationProxyTest < Mocha::TestCase
|
|
102
102
|
end
|
103
103
|
assert_passed(test_result)
|
104
104
|
end
|
105
|
-
|
105
|
+
# rubocop:enable Lint/DuplicateMethods
|
106
106
|
end
|
@@ -2,7 +2,6 @@ require File.expand_path('../acceptance_test_helper', __FILE__)
|
|
2
2
|
require 'mocha/setup'
|
3
3
|
|
4
4
|
class StubClassMethodDefinedOnClassTest < Mocha::TestCase
|
5
|
-
|
6
5
|
include AcceptanceTest
|
7
6
|
|
8
7
|
def setup
|
@@ -13,6 +12,7 @@ class StubClassMethodDefinedOnClassTest < Mocha::TestCase
|
|
13
12
|
teardown_acceptance_test
|
14
13
|
end
|
15
14
|
|
15
|
+
# rubocop:disable Lint/DuplicateMethods
|
16
16
|
def test_should_stub_public_method_and_leave_it_unchanged_after_test
|
17
17
|
klass = Class.new do
|
18
18
|
class << self
|
@@ -75,4 +75,5 @@ class StubClassMethodDefinedOnClassTest < Mocha::TestCase
|
|
75
75
|
end
|
76
76
|
assert_equal :original_return_value, klass.send(:my_class_method)
|
77
77
|
end
|
78
|
+
# rubocop:enable Lint/DuplicateMethods
|
78
79
|
end
|