mocha 0.10.5 → 1.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/FUNDING.yml +1 -0
- data/.rubocop.yml +61 -0
- data/.rubocop_todo.yml +27 -0
- data/.yardopts +24 -0
- data/CONTRIBUTING.md +7 -0
- data/COPYING.md +3 -0
- data/Gemfile +2 -2
- data/{MIT-LICENSE.rdoc → MIT-LICENSE.md} +1 -1
- data/README.md +363 -0
- data/{RELEASE.rdoc → RELEASE.md} +436 -35
- data/Rakefile +87 -87
- data/gemfiles/Gemfile.minitest.latest +2 -2
- data/gemfiles/Gemfile.test-unit.latest +6 -2
- data/init.rb +1 -3
- data/lib/mocha/any_instance_method.rb +12 -45
- data/lib/mocha/api.rb +199 -131
- data/lib/mocha/argument_iterator.rb +6 -10
- data/lib/mocha/backtrace_filter.rb +1 -5
- data/lib/mocha/block_matcher.rb +31 -0
- data/lib/mocha/cardinality.rb +77 -66
- data/lib/mocha/central.rb +27 -18
- data/lib/mocha/change_state_side_effect.rb +3 -7
- data/lib/mocha/class_methods.rb +62 -0
- data/lib/mocha/configuration.rb +399 -46
- data/lib/mocha/debug.rb +12 -0
- data/lib/mocha/deprecation.rb +11 -12
- data/lib/mocha/detection/mini_test.rb +23 -0
- data/lib/mocha/detection/test_unit.rb +27 -0
- data/lib/mocha/error_with_filtered_backtrace.rb +13 -0
- data/lib/mocha/exception_raiser.rb +8 -10
- data/lib/mocha/expectation.rb +290 -151
- data/lib/mocha/expectation_error.rb +6 -13
- data/lib/mocha/expectation_error_factory.rb +35 -0
- data/lib/mocha/expectation_list.rb +22 -22
- data/lib/mocha/hooks.rb +42 -0
- data/lib/mocha/in_state_ordering_constraint.rb +3 -7
- data/lib/mocha/inspect.rb +35 -43
- data/lib/mocha/instance_method.rb +12 -21
- data/lib/mocha/integration/assertion_counter.rb +13 -0
- data/lib/mocha/integration/mini_test/adapter.rb +52 -0
- data/lib/mocha/integration/mini_test/exception_translation.rb +1 -7
- data/lib/mocha/integration/mini_test/nothing.rb +19 -0
- data/lib/mocha/integration/mini_test/version_13.rb +35 -25
- data/lib/mocha/integration/mini_test/version_140.rb +35 -26
- data/lib/mocha/integration/mini_test/version_141.rb +43 -34
- data/lib/mocha/integration/mini_test/version_142_to_172.rb +44 -35
- data/lib/mocha/integration/mini_test/version_200.rb +45 -36
- data/lib/mocha/integration/mini_test/version_201_to_222.rb +44 -35
- data/lib/mocha/integration/mini_test/version_2110_to_2111.rb +70 -0
- data/lib/mocha/integration/mini_test/version_2112_to_320.rb +73 -0
- data/lib/mocha/integration/mini_test/version_230_to_2101.rb +68 -0
- data/lib/mocha/integration/mini_test.rb +51 -49
- data/lib/mocha/integration/monkey_patcher.rb +24 -0
- data/lib/mocha/integration/test_unit/adapter.rb +50 -0
- data/lib/mocha/integration/test_unit/gem_version_200.rb +39 -29
- data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +39 -29
- data/lib/mocha/integration/test_unit/gem_version_203_to_220.rb +39 -29
- data/lib/mocha/integration/test_unit/gem_version_230_to_250.rb +68 -0
- data/lib/mocha/integration/test_unit/nothing.rb +19 -0
- data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +39 -29
- data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +40 -30
- data/lib/mocha/integration/test_unit.rb +45 -51
- data/lib/mocha/integration.rb +6 -33
- data/lib/mocha/invocation.rb +77 -0
- data/lib/mocha/is_a.rb +0 -2
- data/lib/mocha/logger.rb +2 -6
- data/lib/mocha/macos_version.rb +5 -0
- data/lib/mocha/method_matcher.rb +6 -10
- data/lib/mocha/minitest.rb +8 -0
- data/lib/mocha/mock.rb +266 -79
- data/lib/mocha/mockery.rb +104 -106
- data/lib/mocha/names.rb +10 -20
- data/lib/mocha/not_initialized_error.rb +7 -0
- data/lib/mocha/object_methods.rb +169 -0
- data/lib/mocha/parameter_matchers/all_of.rb +18 -14
- data/lib/mocha/parameter_matchers/any_of.rb +19 -14
- data/lib/mocha/parameter_matchers/any_parameters.rb +14 -13
- data/lib/mocha/parameter_matchers/anything.rb +17 -14
- data/lib/mocha/parameter_matchers/base.rb +33 -31
- data/lib/mocha/parameter_matchers/equals.rb +18 -13
- data/lib/mocha/parameter_matchers/equivalent_uri.rb +58 -0
- data/lib/mocha/parameter_matchers/has_entries.rb +19 -14
- data/lib/mocha/parameter_matchers/has_entry.rb +58 -26
- data/lib/mocha/parameter_matchers/has_key.rb +18 -13
- data/lib/mocha/parameter_matchers/has_keys.rb +53 -0
- data/lib/mocha/parameter_matchers/has_value.rb +18 -13
- data/lib/mocha/parameter_matchers/includes.rb +80 -19
- data/lib/mocha/parameter_matchers/instance_methods.rb +18 -0
- data/lib/mocha/parameter_matchers/instance_of.rb +18 -13
- data/lib/mocha/parameter_matchers/is_a.rb +20 -14
- data/lib/mocha/parameter_matchers/kind_of.rb +20 -13
- data/lib/mocha/parameter_matchers/not.rb +19 -14
- data/lib/mocha/parameter_matchers/optionally.rb +23 -17
- data/lib/mocha/parameter_matchers/regexp_matches.rb +16 -12
- data/lib/mocha/parameter_matchers/responds_with.rb +17 -11
- data/lib/mocha/parameter_matchers/yaml_equivalent.rb +15 -9
- data/lib/mocha/parameter_matchers.rb +4 -5
- data/lib/mocha/parameters_matcher.rb +11 -14
- data/lib/mocha/raised_exception.rb +11 -0
- data/lib/mocha/receivers.rb +45 -0
- data/lib/mocha/return_values.rb +11 -15
- data/lib/mocha/ruby_version.rb +4 -0
- data/lib/mocha/sequence.rb +21 -17
- data/lib/mocha/setup.rb +14 -0
- data/lib/mocha/single_return_value.rb +5 -8
- data/lib/mocha/singleton_class.rb +9 -0
- data/lib/mocha/state_machine.rb +69 -67
- data/lib/mocha/stubbed_method.rb +125 -0
- data/lib/mocha/stubbing_error.rb +6 -14
- data/lib/mocha/test_unit.rb +8 -0
- data/lib/mocha/thrower.rb +6 -8
- data/lib/mocha/thrown_object.rb +12 -0
- data/lib/mocha/version.rb +1 -1
- data/lib/mocha/yield_parameters.rb +12 -22
- data/lib/mocha.rb +8 -3
- data/mocha.gemspec +43 -34
- data/yard-templates/default/layout/html/google_analytics.erb +8 -0
- data/yard-templates/default/layout/html/setup.rb +5 -0
- metadata +123 -268
- data/COPYING.rdoc +0 -3
- data/README.rdoc +0 -54
- data/examples/misc.rb +0 -43
- data/examples/mocha.rb +0 -25
- data/examples/stubba.rb +0 -64
- data/gemfiles/Gemfile.minitest.1.3.0 +0 -7
- data/gemfiles/Gemfile.minitest.1.4.0 +0 -7
- data/gemfiles/Gemfile.minitest.1.4.1 +0 -7
- data/gemfiles/Gemfile.minitest.1.4.2 +0 -7
- data/gemfiles/Gemfile.minitest.2.0.0 +0 -7
- data/gemfiles/Gemfile.minitest.2.0.1 +0 -7
- data/gemfiles/Gemfile.minitest.2.3.0 +0 -7
- data/gemfiles/Gemfile.test-unit.2.0.0 +0 -8
- data/gemfiles/Gemfile.test-unit.2.0.1 +0 -7
- data/gemfiles/Gemfile.test-unit.2.0.3 +0 -7
- data/lib/mocha/class_method.rb +0 -98
- data/lib/mocha/integration/mini_test/assertion_counter.rb +0 -23
- data/lib/mocha/integration/mini_test/version_230_to_262.rb +0 -59
- data/lib/mocha/integration/test_unit/assertion_counter.rb +0 -23
- data/lib/mocha/integration/test_unit/gem_version_230_to_240.rb +0 -58
- data/lib/mocha/module_method.rb +0 -16
- data/lib/mocha/multiple_yields.rb +0 -20
- data/lib/mocha/no_yields.rb +0 -11
- data/lib/mocha/object.rb +0 -223
- data/lib/mocha/options.rb +0 -1
- data/lib/mocha/parameter_matchers/object.rb +0 -15
- data/lib/mocha/parameter_matchers/query_string.rb +0 -47
- data/lib/mocha/pretty_parameters.rb +0 -28
- data/lib/mocha/single_yield.rb +0 -18
- data/lib/mocha/standalone.rb +0 -1
- data/lib/mocha/unexpected_invocation.rb +0 -18
- data/lib/mocha_standalone.rb +0 -2
- data/lib/stubba.rb +0 -4
- data/test/acceptance/acceptance_test_helper.rb +0 -41
- data/test/acceptance/api_test.rb +0 -139
- data/test/acceptance/bug_18914_test.rb +0 -43
- data/test/acceptance/bug_21465_test.rb +0 -34
- data/test/acceptance/bug_21563_test.rb +0 -25
- data/test/acceptance/exception_rescue_test.rb +0 -55
- data/test/acceptance/expectations_on_multiple_methods_test.rb +0 -55
- data/test/acceptance/expected_invocation_count_test.rb +0 -232
- data/test/acceptance/failure_messages_test.rb +0 -64
- data/test/acceptance/issue_65_test.rb +0 -63
- data/test/acceptance/issue_70_test.rb +0 -55
- data/test/acceptance/minitest_test.rb +0 -162
- data/test/acceptance/mocha_example_test.rb +0 -98
- data/test/acceptance/mocha_test_result_test.rb +0 -84
- data/test/acceptance/mock_test.rb +0 -100
- data/test/acceptance/mock_with_initializer_block_test.rb +0 -51
- data/test/acceptance/mocked_methods_dispatch_test.rb +0 -78
- data/test/acceptance/multiple_expectations_failure_message_test.rb +0 -68
- data/test/acceptance/optional_parameters_test.rb +0 -70
- data/test/acceptance/parameter_matcher_test.rb +0 -300
- data/test/acceptance/partial_mocks_test.rb +0 -47
- data/test/acceptance/raise_exception_test.rb +0 -39
- data/test/acceptance/return_value_test.rb +0 -52
- data/test/acceptance/sequence_test.rb +0 -192
- data/test/acceptance/states_test.rb +0 -70
- data/test/acceptance/stub_any_instance_method_test.rb +0 -198
- data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +0 -106
- data/test/acceptance/stub_class_method_defined_on_class_test.rb +0 -72
- data/test/acceptance/stub_class_method_defined_on_module_test.rb +0 -75
- data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +0 -75
- data/test/acceptance/stub_everything_test.rb +0 -56
- data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +0 -93
- data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +0 -69
- data/test/acceptance/stub_instance_method_defined_on_class_test.rb +0 -66
- data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +0 -75
- data/test/acceptance/stub_instance_method_defined_on_module_test.rb +0 -75
- data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +0 -75
- data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -70
- data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -72
- data/test/acceptance/stub_module_method_test.rb +0 -163
- data/test/acceptance/stub_test.rb +0 -52
- data/test/acceptance/stubba_example_test.rb +0 -102
- data/test/acceptance/stubba_test.rb +0 -15
- data/test/acceptance/stubba_test_result_test.rb +0 -66
- data/test/acceptance/stubbing_error_backtrace_test.rb +0 -64
- data/test/acceptance/stubbing_method_unnecessarily_test.rb +0 -65
- data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +0 -130
- data/test/acceptance/stubbing_non_existent_class_method_test.rb +0 -157
- data/test/acceptance/stubbing_non_existent_instance_method_test.rb +0 -147
- data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +0 -130
- data/test/acceptance/stubbing_non_public_class_method_test.rb +0 -163
- data/test/acceptance/stubbing_non_public_instance_method_test.rb +0 -143
- data/test/acceptance/stubbing_on_non_mock_object_test.rb +0 -64
- data/test/acceptance/throw_test.rb +0 -45
- data/test/acceptance/unstubbing_test.rb +0 -151
- data/test/deprecation_disabler.rb +0 -15
- data/test/execution_point.rb +0 -36
- data/test/method_definer.rb +0 -24
- data/test/mini_test_result.rb +0 -83
- data/test/simple_counter.rb +0 -13
- data/test/test_helper.rb +0 -11
- data/test/test_runner.rb +0 -50
- data/test/unit/any_instance_method_test.rb +0 -136
- data/test/unit/array_inspect_test.rb +0 -16
- data/test/unit/backtrace_filter_test.rb +0 -19
- data/test/unit/cardinality_test.rb +0 -56
- data/test/unit/central_test.rb +0 -100
- data/test/unit/change_state_side_effect_test.rb +0 -41
- data/test/unit/class_method_test.rb +0 -260
- data/test/unit/configuration_test.rb +0 -38
- data/test/unit/date_time_inspect_test.rb +0 -21
- data/test/unit/exception_raiser_test.rb +0 -42
- data/test/unit/expectation_list_test.rb +0 -71
- data/test/unit/expectation_test.rb +0 -480
- data/test/unit/hash_inspect_test.rb +0 -16
- data/test/unit/in_state_ordering_constraint_test.rb +0 -43
- data/test/unit/method_matcher_test.rb +0 -23
- data/test/unit/mock_test.rb +0 -312
- data/test/unit/mockery_test.rb +0 -150
- data/test/unit/multiple_yields_test.rb +0 -18
- data/test/unit/no_yields_test.rb +0 -18
- data/test/unit/object_inspect_test.rb +0 -38
- data/test/unit/object_test.rb +0 -87
- 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/equals_test.rb +0 -25
- data/test/unit/parameter_matchers/has_entries_test.rb +0 -51
- data/test/unit/parameter_matchers/has_entry_test.rb +0 -96
- data/test/unit/parameter_matchers/has_key_test.rb +0 -55
- data/test/unit/parameter_matchers/has_value_test.rb +0 -57
- data/test/unit/parameter_matchers/includes_test.rb +0 -44
- 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 -46
- data/test/unit/parameter_matchers/responds_with_test.rb +0 -25
- data/test/unit/parameter_matchers/stub_matcher.rb +0 -27
- data/test/unit/parameter_matchers/yaml_equivalent_test.rb +0 -25
- 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/single_return_value_test.rb +0 -14
- data/test/unit/single_yield_test.rb +0 -18
- data/test/unit/state_machine_test.rb +0 -98
- data/test/unit/string_inspect_test.rb +0 -11
- data/test/unit/thrower_test.rb +0 -20
- data/test/unit/yield_parameters_test.rb +0 -93
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 77366bffa410729ac6089097db85ded323e74d1ec7bc6eb0ea13ae4a18b44eac
|
4
|
+
data.tar.gz: f9f3c89bbc29393e99ec62e0d1395ed4fa4863d0f790d24bbe6be3537450e4c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 91e277be3c868ef2f6655016a53f040835b8e9d6390c789b6f047be72e9b96726865eefcb161b86d0f8e89ead1c58e9adbf1a1cbcdf9cba8a0f0b536de86222d
|
7
|
+
data.tar.gz: 4b7bf751e350762a5a3d9cd17fbe1a590ff5122a718417b75304fa86f8323b20652a24584cedabd859395aeef004402e57c70c15cd88746032659b0d9df4cccf
|
data/.github/FUNDING.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
github: floehopper
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
TargetRubyVersion: 2.2 # closest to required_ruby_version of '>= 1.8.6'
|
5
|
+
|
6
|
+
# Even the reference in the documentation suggests that you should prefer
|
7
|
+
# `alias_method` vs `alias`, so I don't understand why that isn't the default.
|
8
|
+
Style/Alias:
|
9
|
+
EnforcedStyle: prefer_alias_method
|
10
|
+
|
11
|
+
Style/Documentation:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
# Enumerable#each_with_object only available since Ruby v1.9
|
15
|
+
Style/EachWithObject:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
# Kernel#__dir__ has only been available since Ruby v2.0
|
19
|
+
Style/ExpandPathArguments:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
# Kernel#format has only supported named references since Ruby v1.9
|
23
|
+
Style/FormatStringToken:
|
24
|
+
EnforcedStyle: unannotated
|
25
|
+
|
26
|
+
# ruby19 style has only been supported since Ruby v1.9
|
27
|
+
Style/HashSyntax:
|
28
|
+
EnforcedStyle: hash_rockets
|
29
|
+
|
30
|
+
# I'm not keen on this cop, because it's easy to miss the conditional
|
31
|
+
# I think the results are particularly unhelpful when Metrics/LineLength is big
|
32
|
+
Style/IfUnlessModifier:
|
33
|
+
Enabled: false
|
34
|
+
|
35
|
+
# Lambda literal syntax has only been supported since Ruby v2.0
|
36
|
+
Style/Lambda:
|
37
|
+
EnforcedStyle: lambda
|
38
|
+
|
39
|
+
# Symbol array literal syntax has only been supported since Ruby v2.0
|
40
|
+
Style/SymbolArray:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
# I'm not keen on this cop, because it's easy to miss the while/until
|
44
|
+
Style/WhileUntilModifier:
|
45
|
+
Enabled: false
|
46
|
+
|
47
|
+
# This recently introduced cop seems to have stirred up some controversy
|
48
|
+
Style/AccessModifierDeclarations:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
# Enabling this cop results in an "Infinite loop detected" exception
|
52
|
+
Layout/AccessModifierIndentation:
|
53
|
+
Enabled: false
|
54
|
+
|
55
|
+
# Allow long comment lines, e.g. YARD documentation
|
56
|
+
Metrics/LineLength:
|
57
|
+
IgnoredPatterns: ['\A\s*#']
|
58
|
+
|
59
|
+
# It's not possible to set TargetRubyVersion to Ruby < v2.2
|
60
|
+
Gemspec/RequiredRubyVersion:
|
61
|
+
Enabled: false
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2019-11-16 18:15:36 +0000 using RuboCop version 0.58.2.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 57
|
10
|
+
Metrics/AbcSize:
|
11
|
+
Max: 26
|
12
|
+
|
13
|
+
# Offense count: 23
|
14
|
+
# Configuration parameters: CountComments.
|
15
|
+
Metrics/ClassLength:
|
16
|
+
Max: 366
|
17
|
+
|
18
|
+
# Offense count: 172
|
19
|
+
# Configuration parameters: CountComments.
|
20
|
+
Metrics/MethodLength:
|
21
|
+
Max: 31
|
22
|
+
|
23
|
+
# Offense count: 545
|
24
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
25
|
+
# URISchemes: http, https
|
26
|
+
Metrics/LineLength:
|
27
|
+
Max: 180
|
data/.yardopts
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
--output-dir docs
|
2
|
+
--template-path yard-templates
|
3
|
+
--no-private
|
4
|
+
lib/mocha/api.rb
|
5
|
+
lib/mocha/hooks.rb
|
6
|
+
lib/mocha/mock.rb
|
7
|
+
lib/mocha/expectation.rb
|
8
|
+
lib/mocha/object_methods.rb
|
9
|
+
lib/mocha/class_methods.rb
|
10
|
+
lib/mocha/parameter_matchers.rb
|
11
|
+
lib/mocha/parameter_matchers
|
12
|
+
lib/mocha/state_machine.rb
|
13
|
+
lib/mocha/sequence.rb
|
14
|
+
lib/mocha/configuration.rb
|
15
|
+
lib/mocha/expectation_error_factory.rb
|
16
|
+
lib/mocha/expectation_error.rb
|
17
|
+
lib/mocha/stubbing_error.rb
|
18
|
+
lib/mocha/unexpected_invocation.rb
|
19
|
+
lib/mocha/integration/test_unit/adapter.rb
|
20
|
+
lib/mocha/integration/mini_test/adapter.rb
|
21
|
+
-
|
22
|
+
RELEASE.md
|
23
|
+
COPYING.md
|
24
|
+
MIT-LICENSE.md
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
* Pull requests are welcomed.
|
2
|
+
* Fork the repository.
|
3
|
+
* Make your changes in a branch.
|
4
|
+
* Add/modify/remove tests as appropriate.
|
5
|
+
* Open a pull request based on a branch on your fork.
|
6
|
+
* Wait for your pull request build to pass on [Circle CI](https://app.circleci.com/pipelines/github/freerange/mocha).
|
7
|
+
* Pull requests with failing tests will not be accepted.
|
data/COPYING.md
ADDED
data/Gemfile
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
gemspec
|
3
|
+
gemspec
|
@@ -4,4 +4,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
4
4
|
|
5
5
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
6
|
|
7
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,363 @@
|
|
1
|
+
## Mocha [![CircleCI status of freerange/mocha](https://circleci.com/gh/freerange/mocha.svg?style=shield&circle-token=bc1f6576c77da43ec58badde9273fa4eb1d7f53a)](https://app.circleci.com/pipelines/github/freerange/mocha) [![Gem Version](https://badge.fury.io/rb/mocha.svg)](http://badge.fury.io/rb/mocha) [![SemVer](https://api.dependabot.com/badges/compatibility_score?dependency-name=mocha&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=mocha&package-manager=bundler&version-scheme=semver)
|
2
|
+
|
3
|
+
### Description
|
4
|
+
|
5
|
+
* A Ruby library for [mocking](http://xunitpatterns.com/Mock%20Object.html) and [stubbing](http://xunitpatterns.com/Test%20Stub.html) - but deliberately not (yet) [faking](http://xunitpatterns.com/Fake%20Object.html) or [spying](http://xunitpatterns.com/Test%20Spy.html).
|
6
|
+
* A unified, simple and readable syntax for both full & partial mocking.
|
7
|
+
* Built-in support for MiniTest and Test::Unit.
|
8
|
+
* Supported by many other test frameworks.
|
9
|
+
|
10
|
+
### Intended Usage
|
11
|
+
Mocha is intended to be used in unit tests for the [Mock Object](http://xunitpatterns.com/Mock%20Object.html) or [Test Stub](http://xunitpatterns.com/Test%20Stub.html) types of [Test Double](http://xunitpatterns.com/Test%20Double.html), not the [Fake Object](http://xunitpatterns.com/Fake%20Object.html) or [Test Spy](http://xunitpatterns.com/Test%20Spy.html) types. Although it would be possible to extend Mocha to allow the implementation of fakes and spies, we have chosen to keep it focused on mocks and stubs.
|
12
|
+
|
13
|
+
### Installation
|
14
|
+
|
15
|
+
#### Gem
|
16
|
+
|
17
|
+
Install the latest version of the gem with the following command...
|
18
|
+
|
19
|
+
$ gem install mocha
|
20
|
+
|
21
|
+
Note: If you are intending to use Mocha with Test::Unit or MiniTest, you should only setup Mocha *after* loading the relevant test library...
|
22
|
+
|
23
|
+
##### Test::Unit
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'rubygems'
|
27
|
+
gem 'mocha'
|
28
|
+
require 'test/unit'
|
29
|
+
require 'mocha/test_unit'
|
30
|
+
```
|
31
|
+
|
32
|
+
##### MiniTest
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require 'rubygems'
|
36
|
+
gem 'mocha'
|
37
|
+
require 'minitest/unit'
|
38
|
+
require 'mocha/minitest'
|
39
|
+
```
|
40
|
+
|
41
|
+
#### Bundler
|
42
|
+
|
43
|
+
If you're using Bundler, include Mocha in the `Gemfile` and then setup Mocha later once you know the test library has been loaded...
|
44
|
+
|
45
|
+
##### Test::Unit
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
# Gemfile
|
49
|
+
gem 'mocha'
|
50
|
+
|
51
|
+
# Elsewhere after Bundler has loaded gems e.g. after `require 'bundler/setup'`
|
52
|
+
require 'test/unit'
|
53
|
+
require 'mocha/test_unit'
|
54
|
+
```
|
55
|
+
|
56
|
+
##### MiniTest
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
# Gemfile
|
60
|
+
gem 'mocha'
|
61
|
+
|
62
|
+
# Elsewhere after Bundler has loaded gems e.g. after `require 'bundler/setup'`
|
63
|
+
require 'minitest/unit'
|
64
|
+
require 'mocha/minitest'
|
65
|
+
```
|
66
|
+
|
67
|
+
##### RSpec
|
68
|
+
|
69
|
+
RSpec includes a mocha adapter. Just tell RSpec you want to mock with `:mocha`:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
# Gemfile in Rails app
|
73
|
+
gem 'mocha'
|
74
|
+
|
75
|
+
# Within `spec/spec_helper.rb`
|
76
|
+
RSpec.configure do |config|
|
77
|
+
config.mock_with :mocha
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
Note: There is no need to use a require statement to setup Mocha; RSpec does this itself.
|
82
|
+
|
83
|
+
##### Cucumber
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
# In e.g. features/support/mocha.rb
|
87
|
+
require 'mocha/api'
|
88
|
+
|
89
|
+
World(Mocha::API)
|
90
|
+
|
91
|
+
Around do |scenario, block|
|
92
|
+
begin
|
93
|
+
mocha_setup
|
94
|
+
block.call
|
95
|
+
mocha_verify
|
96
|
+
ensure
|
97
|
+
mocha_teardown
|
98
|
+
end
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
#### Rails
|
103
|
+
|
104
|
+
If you're loading Mocha using Bundler within a Rails application, you should setup Mocha manually e.g. at the bottom of your `test_helper.rb`.
|
105
|
+
|
106
|
+
##### MiniTest
|
107
|
+
|
108
|
+
Note that since Rails v4 (at least), `ActiveSupport::TestCase` has inherited from `Minitest::Test` or its earlier equivalents. Thus unless you are *explicitly* using Test::Unit, you are likely to be using MiniTest.
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
# Gemfile in Rails app
|
112
|
+
gem 'mocha'
|
113
|
+
|
114
|
+
# At bottom of test_helper.rb (or at least after `require 'rails/test_help'`)
|
115
|
+
require 'mocha/minitest'
|
116
|
+
```
|
117
|
+
|
118
|
+
##### Other Test Framework
|
119
|
+
|
120
|
+
Follow the instructions for the relevant test framework in the [Bundler](#bundler) section, but ensure that the relevant Mocha file (`mocha/minitest`, `mocha/test_unit`, or `mocha/api`) is required **after** the test framework has been loaded, e.g. at the bottom of `test_helper.rb` or `spec_helper.rb`, or at least after `rails/test_help` has been required.
|
121
|
+
|
122
|
+
#### Known Issues
|
123
|
+
|
124
|
+
* In Mocha v1.10.0 an undocumented feature of `API#mock`, `API#stub` & `API#stub_everything` was changed. Previously when these methods were passed a single symbol, they returned a mock object that responded to the method identified by the symbol. Now Passing a single symbol is equivalent to passing a single string, i.e. it now defines the 'name' of the mock object.
|
125
|
+
* In Mocha v1.2.0 there is a scenario where stubbing a class method originally defined in a module hangs the Ruby interpreter due to [a bug in Ruby v2.3.1](https://bugs.ruby-lang.org/issues/12832). See #272. This was fixed in Mocha v1.2.1.
|
126
|
+
* Since v1.1.0 Mocha has used prepended modules internally for stubbing methods. There is [an obscure Ruby bug](https://bugs.ruby-lang.org/issues/12876) in many (but not all) versions of Ruby between v2.0 & v2.3 which under certain circumstances may cause your Ruby interpreter to hang. See the Ruby bug report for more details. The bug has been fixed in Ruby v2.3.3 & v2.4.0.
|
127
|
+
* Stubbing an aliased class method, where the original method is defined in a module that's used to `extend` the class doesn't work in Ruby 1.8.x. See stub_method_defined_on_module_and_aliased_test.rb for an example of this behaviour.
|
128
|
+
* 0.13.x versions cause a harmless, but annoying, deprecation warning when used with Rails 3.2.0-3.2.12, 3.1.0-3.1.10 & 3.0.0-3.0.19.
|
129
|
+
* 0.11.x versions don't work with Rails 3.2.13 (`TypeError: superclass mismatch for class ExpectationError`). See #115.
|
130
|
+
* Versions 0.10.2, 0.10.3 & 0.11.0 of the Mocha gem were broken. Please do not use these versions.
|
131
|
+
|
132
|
+
### Usage
|
133
|
+
|
134
|
+
#### Quick Start
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
require 'test/unit'
|
138
|
+
require 'mocha/test_unit'
|
139
|
+
|
140
|
+
class MiscExampleTest < Test::Unit::TestCase
|
141
|
+
def test_mocking_a_class_method
|
142
|
+
product = Product.new
|
143
|
+
Product.expects(:find).with(1).returns(product)
|
144
|
+
assert_equal product, Product.find(1)
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_mocking_an_instance_method_on_a_real_object
|
148
|
+
product = Product.new
|
149
|
+
product.expects(:save).returns(true)
|
150
|
+
assert product.save
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_stubbing_instance_methods_on_real_objects
|
154
|
+
prices = [stub(:pence => 1000), stub(:pence => 2000)]
|
155
|
+
product = Product.new
|
156
|
+
product.stubs(:prices).returns(prices)
|
157
|
+
assert_equal [1000, 2000], product.prices.collect {|p| p.pence}
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_stubbing_an_instance_method_on_all_instances_of_a_class
|
161
|
+
Product.any_instance.stubs(:name).returns('stubbed_name')
|
162
|
+
product = Product.new
|
163
|
+
assert_equal 'stubbed_name', product.name
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_traditional_mocking
|
167
|
+
object = mock('object')
|
168
|
+
object.expects(:expected_method).with(:p1, :p2).returns(:result)
|
169
|
+
assert_equal :result, object.expected_method(:p1, :p2)
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_shortcuts
|
173
|
+
object = stub(:method1 => :result1, :method2 => :result2)
|
174
|
+
assert_equal :result1, object.method1
|
175
|
+
assert_equal :result2, object.method2
|
176
|
+
end
|
177
|
+
end
|
178
|
+
```
|
179
|
+
|
180
|
+
#### Mock Objects
|
181
|
+
|
182
|
+
```ruby
|
183
|
+
class Enterprise
|
184
|
+
def initialize(dilithium)
|
185
|
+
@dilithium = dilithium
|
186
|
+
end
|
187
|
+
|
188
|
+
def go(warp_factor)
|
189
|
+
warp_factor.times { @dilithium.nuke(:anti_matter) }
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
require 'test/unit'
|
194
|
+
require 'mocha/test_unit'
|
195
|
+
|
196
|
+
class EnterpriseTest < Test::Unit::TestCase
|
197
|
+
def test_should_boldly_go
|
198
|
+
dilithium = mock()
|
199
|
+
dilithium.expects(:nuke).with(:anti_matter).at_least_once # auto-verified at end of test
|
200
|
+
enterprise = Enterprise.new(dilithium)
|
201
|
+
enterprise.go(2)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
```
|
205
|
+
|
206
|
+
#### Partial Mocking
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
class Order
|
210
|
+
attr_accessor :shipped_on
|
211
|
+
|
212
|
+
def total_cost
|
213
|
+
line_items.inject(0) { |total, line_item| total + line_item.price } + shipping_cost
|
214
|
+
end
|
215
|
+
|
216
|
+
def total_weight
|
217
|
+
line_items.inject(0) { |total, line_item| total + line_item.weight }
|
218
|
+
end
|
219
|
+
|
220
|
+
def shipping_cost
|
221
|
+
total_weight * 5 + 10
|
222
|
+
end
|
223
|
+
|
224
|
+
class << self
|
225
|
+
def find_all
|
226
|
+
# Database.connection.execute('select * from orders...
|
227
|
+
end
|
228
|
+
|
229
|
+
def number_shipped_since(date)
|
230
|
+
find_all.select { |order| order.shipped_on > date }.length
|
231
|
+
end
|
232
|
+
|
233
|
+
def unshipped_value
|
234
|
+
find_all.inject(0) { |total, order| order.shipped_on ? total : total + order.total_cost }
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
require 'test/unit'
|
240
|
+
require 'mocha/test_unit'
|
241
|
+
|
242
|
+
class OrderTest < Test::Unit::TestCase
|
243
|
+
# illustrates stubbing instance method
|
244
|
+
def test_should_calculate_shipping_cost_based_on_total_weight
|
245
|
+
order = Order.new
|
246
|
+
order.stubs(:total_weight).returns(10)
|
247
|
+
assert_equal 60, order.shipping_cost
|
248
|
+
end
|
249
|
+
|
250
|
+
# illustrates stubbing class method
|
251
|
+
def test_should_count_number_of_orders_shipped_after_specified_date
|
252
|
+
now = Time.now; week_in_secs = 7 * 24 * 60 * 60
|
253
|
+
order_1 = Order.new; order_1.shipped_on = now - 1 * week_in_secs
|
254
|
+
order_2 = Order.new; order_2.shipped_on = now - 3 * week_in_secs
|
255
|
+
Order.stubs(:find_all).returns([order_1, order_2])
|
256
|
+
assert_equal 1, Order.number_shipped_since(now - 2 * week_in_secs)
|
257
|
+
end
|
258
|
+
|
259
|
+
# illustrates stubbing instance method for all instances of a class
|
260
|
+
def test_should_calculate_value_of_unshipped_orders
|
261
|
+
Order.stubs(:find_all).returns([Order.new, Order.new, Order.new])
|
262
|
+
Order.any_instance.stubs(:shipped_on).returns(nil)
|
263
|
+
Order.any_instance.stubs(:total_cost).returns(10)
|
264
|
+
assert_equal 30, Order.unshipped_value
|
265
|
+
end
|
266
|
+
end
|
267
|
+
```
|
268
|
+
|
269
|
+
### Thread safety
|
270
|
+
|
271
|
+
Mocha currently *does not* attempt to be thread-safe.
|
272
|
+
|
273
|
+
#### Can I test multi-threaded code with Mocha?
|
274
|
+
|
275
|
+
The short answer is no. In multi-threaded code Mocha exceptions may be raised in a thread other than the one which is running the test and thus a Mocha exception may not be correctly intercepted by Mocha exception handling code.
|
276
|
+
|
277
|
+
#### Can I run my tests across multiple threads?
|
278
|
+
|
279
|
+
Maybe, but probably not. Partial mocking changes the state of objects in the `ObjectSpace` which is shared across all threads in the Ruby process and this access to what is effectively global state is not synchronized. So, for example, if two tests are running concurrently and one uses `#any_instance` to modify a class, both tests will see those changes immediately.
|
280
|
+
|
281
|
+
### Expectation matching / invocation order
|
282
|
+
|
283
|
+
Stubs and expectations are basically the same thing. A stub is just an expectation of zero or more invocations. The `Expectation#stubs` method is syntactic sugar to make the intent of the test more explicit.
|
284
|
+
|
285
|
+
When a method is invoked on a mock object, the mock object searches through its expectations from newest to oldest to find one that matches the invocation. After the invocation, the matching expectation might stop matching further invocations.
|
286
|
+
|
287
|
+
See the [documentation](https://mocha.jamesmead.org/Mocha/Mock.html) for `Mocha::Mock` for further details.
|
288
|
+
|
289
|
+
### Configuration
|
290
|
+
|
291
|
+
If you want, Mocha can generate a warning or raise an exception when:
|
292
|
+
|
293
|
+
* stubbing a method unnecessarily
|
294
|
+
* stubbing method on a non-mock object
|
295
|
+
* stubbing a non-existent method
|
296
|
+
* stubbing a non-public method
|
297
|
+
|
298
|
+
See the [documentation](https://mocha.jamesmead.org/Mocha/Configuration.html) for `Mocha::Configuration` for further details.
|
299
|
+
|
300
|
+
##### MOCHA_OPTIONS
|
301
|
+
`MOCHA_OPTIONS` is an environment variable whose value can be set to a comma-separated list, so that we can specify multiple options e.g. `MOCHA_OPTIONS=debug,use_test_unit_gem`.
|
302
|
+
Only the following values are currently recognized and have an effect:
|
303
|
+
* `debug`: Enables a debug mode which will output backtraces for each deprecation warning. This is useful for finding where in the test suite the deprecated calls are.
|
304
|
+
|
305
|
+
### Useful Links
|
306
|
+
|
307
|
+
* [Official Documentation](https://mocha.jamesmead.org)
|
308
|
+
* [Source Code](http://github.com/freerange/mocha)
|
309
|
+
* [Mailing List](http://groups.google.com/group/mocha-developer)
|
310
|
+
* [James Mead's Blog](http://jamesmead.org/blog/)
|
311
|
+
* [An Introduction To Mock Objects In Ruby](http://jamesmead.org/talks/2007-07-09-introduction-to-mock-objects-in-ruby-at-lrug/)
|
312
|
+
* [Mocks Aren't Stubs](http://martinfowler.com/articles/mocksArentStubs.html)
|
313
|
+
* [Growing Object-Oriented Software Guided By Tests](http://www.growing-object-oriented-software.com/)
|
314
|
+
* [Mock Roles Not Objects](http://www.jmock.org/oopsla2004.pdf)
|
315
|
+
* [jMock](http://www.jmock.org/)
|
316
|
+
|
317
|
+
### Contributors
|
318
|
+
|
319
|
+
See this [list of contributors](https://github.com/freerange/mocha/graphs/contributors).
|
320
|
+
|
321
|
+
### Releasing a new version
|
322
|
+
|
323
|
+
* Update the RELEASE.md file with a summary of changes
|
324
|
+
* Bump the version in `lib/mocha/version.rb`
|
325
|
+
* Commit & push to GitHub
|
326
|
+
* Check CircleCI build is passing - https://app.circleci.com/pipelines/github/freerange/mocha
|
327
|
+
|
328
|
+
* Sign in to Google Analytics - https://analytics.google.com/analytics/web/
|
329
|
+
* Find the web property ID for floehopper (625523) > Mocha Documentation (UA-625523-7)
|
330
|
+
* Generate documentation:
|
331
|
+
|
332
|
+
```bash
|
333
|
+
$ MOCHA_GENERATE_DOCS=true bundle install
|
334
|
+
|
335
|
+
$ MOCHA_GENERATE_DOCS=true GOOGLE_ANALYTICS_WEB_PROPERTY_ID=UA-625523-7 rake generate_docs
|
336
|
+
```
|
337
|
+
* Commit documentation & push to GitHub
|
338
|
+
* Sign in to rubygems.org and find API key - https://rubygems.org/profile/edit
|
339
|
+
|
340
|
+
```bash
|
341
|
+
$ curl -u james@floehopper.org https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials; chmod 0600 ~/.gem/credentials`
|
342
|
+
```
|
343
|
+
|
344
|
+
* Release gem to Rubygems:
|
345
|
+
|
346
|
+
```bash
|
347
|
+
$ rake release
|
348
|
+
[runs tests]
|
349
|
+
mocha 1.2.0 built to pkg/mocha-1.2.0.gem.
|
350
|
+
Tagged v1.2.0.
|
351
|
+
Pushed git commits and tags.
|
352
|
+
Pushed mocha 1.2.0 to rubygems.org.
|
353
|
+
```
|
354
|
+
|
355
|
+
### History
|
356
|
+
|
357
|
+
Mocha was initially harvested from projects at [Reevoo](http://www.reevoo.com/). It's syntax is heavily based on that of [jMock](http://www.jmock.org).
|
358
|
+
|
359
|
+
### License
|
360
|
+
|
361
|
+
© Copyright Revieworld Ltd. 2006
|
362
|
+
|
363
|
+
You may use, copy and redistribute this library under the same terms as [Ruby itself](http://www.ruby-lang.org/en/LICENSE.txt) or under the [MIT license](http://www.opensource.org/licenses/MIT).
|