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
data/lib/mocha/object.rb
DELETED
@@ -1,223 +0,0 @@
|
|
1
|
-
require 'mocha/mockery'
|
2
|
-
require 'mocha/instance_method'
|
3
|
-
require 'mocha/class_method'
|
4
|
-
require 'mocha/module_method'
|
5
|
-
require 'mocha/any_instance_method'
|
6
|
-
require 'mocha/argument_iterator'
|
7
|
-
|
8
|
-
module Mocha
|
9
|
-
|
10
|
-
# Methods added to all objects to allow mocking and stubbing on real objects.
|
11
|
-
#
|
12
|
-
# Methods return a Mocha::Expectation which can be further modified by methods on Mocha::Expectation.
|
13
|
-
module ObjectMethods
|
14
|
-
|
15
|
-
def mocha # :nodoc:
|
16
|
-
@mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
|
17
|
-
end
|
18
|
-
|
19
|
-
def reset_mocha # :nodoc:
|
20
|
-
@mocha = nil
|
21
|
-
end
|
22
|
-
|
23
|
-
def stubba_method # :nodoc:
|
24
|
-
Mocha::InstanceMethod
|
25
|
-
end
|
26
|
-
|
27
|
-
def stubba_object # :nodoc:
|
28
|
-
self
|
29
|
-
end
|
30
|
-
|
31
|
-
# :call-seq: expects(method_name) -> expectation
|
32
|
-
# expects(method_names_vs_return_values) -> last expectation
|
33
|
-
#
|
34
|
-
# Adds an expectation that a method identified by +method_name+ Symbol must be called exactly once with any parameters.
|
35
|
-
# Returns the new expectation which can be further modified by methods on Mocha::Expectation.
|
36
|
-
# product = Product.new
|
37
|
-
# product.expects(:save).returns(true)
|
38
|
-
# assert_equal true, product.save
|
39
|
-
#
|
40
|
-
# The original implementation of <tt>Product#save</tt> is replaced temporarily.
|
41
|
-
#
|
42
|
-
# The original implementation of <tt>Product#save</tt> is restored at the end of the test.
|
43
|
-
#
|
44
|
-
# If +method_names_vs_return_values+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
|
45
|
-
# product = Product.new
|
46
|
-
# product.expects(:valid? => true, :save => true)
|
47
|
-
#
|
48
|
-
# # exactly equivalent to
|
49
|
-
#
|
50
|
-
# product = Product.new
|
51
|
-
# product.expects(:valid?).returns(true)
|
52
|
-
# product.expects(:save).returns(true)
|
53
|
-
def expects(method_name_or_hash)
|
54
|
-
if method_name_or_hash.to_s =~ /the[^a-z]*spanish[^a-z]*inquisition/i
|
55
|
-
raise Mocha::ExpectationError.new('NOBODY EXPECTS THE SPANISH INQUISITION!')
|
56
|
-
end
|
57
|
-
expectation = nil
|
58
|
-
mockery = Mocha::Mockery.instance
|
59
|
-
iterator = ArgumentIterator.new(method_name_or_hash)
|
60
|
-
iterator.each { |*args|
|
61
|
-
method_name = args.shift
|
62
|
-
mockery.on_stubbing(self, method_name)
|
63
|
-
method = stubba_method.new(stubba_object, method_name)
|
64
|
-
mockery.stubba.stub(method)
|
65
|
-
expectation = mocha.expects(method_name, caller)
|
66
|
-
expectation.returns(args.shift) if args.length > 0
|
67
|
-
}
|
68
|
-
expectation
|
69
|
-
end
|
70
|
-
|
71
|
-
# :call-seq: stubs(method_name) -> expectation
|
72
|
-
# stubs(method_names_vs_return_values) -> last expectation
|
73
|
-
#
|
74
|
-
# Adds an expectation that a method identified by +method_name+ Symbol may be called any number of times with any parameters.
|
75
|
-
# Returns the new expectation which can be further modified by methods on Mocha::Expectation.
|
76
|
-
# product = Product.new
|
77
|
-
# product.stubs(:save).returns(true)
|
78
|
-
# assert_equal true, product.save
|
79
|
-
#
|
80
|
-
# The original implementation of <tt>Product#save</tt> is replaced temporarily.
|
81
|
-
#
|
82
|
-
# The original implementation of <tt>Product#save</tt> is restored at the end of the test.
|
83
|
-
#
|
84
|
-
# If +method_names_vs_return_values+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
|
85
|
-
# product = Product.new
|
86
|
-
# product.stubs(:valid? => true, :save => true)
|
87
|
-
#
|
88
|
-
# # exactly equivalent to
|
89
|
-
#
|
90
|
-
# product = Product.new
|
91
|
-
# product.stubs(:valid?).returns(true)
|
92
|
-
# product.stubs(:save).returns(true)
|
93
|
-
def stubs(method_name_or_hash)
|
94
|
-
expectation = nil
|
95
|
-
mockery = Mocha::Mockery.instance
|
96
|
-
iterator = ArgumentIterator.new(method_name_or_hash)
|
97
|
-
iterator.each { |*args|
|
98
|
-
method_name = args.shift
|
99
|
-
mockery.on_stubbing(self, method_name)
|
100
|
-
method = stubba_method.new(stubba_object, method_name)
|
101
|
-
mockery.stubba.stub(method)
|
102
|
-
expectation = mocha.stubs(method_name, caller)
|
103
|
-
expectation.returns(args.shift) if args.length > 0
|
104
|
-
}
|
105
|
-
expectation
|
106
|
-
end
|
107
|
-
|
108
|
-
# :call-seq: unstub(*method_names)
|
109
|
-
#
|
110
|
-
# Removes the method stub added by calls to #expects or #stubs.
|
111
|
-
# Restores the original behaviour of the method before it was stubbed.
|
112
|
-
# multiplier = Multiplier.new
|
113
|
-
# multiplier.double(2) # => 4
|
114
|
-
# multiplier.stubs(:double).raises
|
115
|
-
# multiplier.double(2) # => raises exception
|
116
|
-
# multiplier.unstub(:double)
|
117
|
-
# multiplier.double(2) # => 4
|
118
|
-
#
|
119
|
-
# The original implementation of <tt>Multiplier#double</tt> is replaced temporarily.
|
120
|
-
#
|
121
|
-
# The original implementation of <tt>Multiplier#double</tt> is restored when #unstub is called.
|
122
|
-
#
|
123
|
-
# WARNING: If you #unstub a method which still has unsatisfied expectations, you may be removing
|
124
|
-
# the only way those expectations can be satisfied. Use #unstub with care.
|
125
|
-
#
|
126
|
-
# If multiple +method_names+ are supplied, each method is unstubbed.
|
127
|
-
# multiplier.unstub(:double, :triple)
|
128
|
-
#
|
129
|
-
# # exactly equivalent to
|
130
|
-
#
|
131
|
-
# multiplier.unstub(:double)
|
132
|
-
# multiplier.unstub(:triple)
|
133
|
-
def unstub(*method_names)
|
134
|
-
mockery = Mocha::Mockery.instance
|
135
|
-
method_names.each do |method_name|
|
136
|
-
method = stubba_method.new(stubba_object, method_name)
|
137
|
-
mockery.stubba.unstub(method)
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
def method_exists?(method, include_public_methods = true) # :nodoc:
|
142
|
-
if include_public_methods
|
143
|
-
return true if public_methods(include_superclass_methods = true).include?(method)
|
144
|
-
return true if respond_to?(method.to_sym)
|
145
|
-
end
|
146
|
-
return true if protected_methods(include_superclass_methods = true).include?(method)
|
147
|
-
return true if private_methods(include_superclass_methods = true).include?(method)
|
148
|
-
return false
|
149
|
-
end
|
150
|
-
|
151
|
-
end
|
152
|
-
|
153
|
-
module ModuleMethods # :nodoc:
|
154
|
-
|
155
|
-
def stubba_method
|
156
|
-
Mocha::ModuleMethod
|
157
|
-
end
|
158
|
-
|
159
|
-
end
|
160
|
-
|
161
|
-
# Methods added all classes to allow mocking and stubbing on real objects.
|
162
|
-
module ClassMethods
|
163
|
-
|
164
|
-
def stubba_method # :nodoc:
|
165
|
-
Mocha::ClassMethod
|
166
|
-
end
|
167
|
-
|
168
|
-
class AnyInstance # :nodoc:
|
169
|
-
|
170
|
-
def initialize(klass)
|
171
|
-
@stubba_object = klass
|
172
|
-
end
|
173
|
-
|
174
|
-
def mocha
|
175
|
-
@mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
|
176
|
-
end
|
177
|
-
|
178
|
-
def stubba_method
|
179
|
-
Mocha::AnyInstanceMethod
|
180
|
-
end
|
181
|
-
|
182
|
-
def stubba_object
|
183
|
-
@stubba_object
|
184
|
-
end
|
185
|
-
|
186
|
-
def method_exists?(method, include_public_methods = true)
|
187
|
-
if include_public_methods
|
188
|
-
return true if @stubba_object.public_instance_methods(include_superclass_methods = true).include?(method)
|
189
|
-
end
|
190
|
-
return true if @stubba_object.protected_instance_methods(include_superclass_methods = true).include?(method)
|
191
|
-
return true if @stubba_object.private_instance_methods(include_superclass_methods = true).include?(method)
|
192
|
-
return false
|
193
|
-
end
|
194
|
-
|
195
|
-
end
|
196
|
-
|
197
|
-
# :call-seq: any_instance -> mock object
|
198
|
-
#
|
199
|
-
# Returns a mock object which will detect calls to any instance of this class.
|
200
|
-
# Product.any_instance.stubs(:save).returns(false)
|
201
|
-
# product_1 = Product.new
|
202
|
-
# assert_equal false, product_1.save
|
203
|
-
# product_2 = Product.new
|
204
|
-
# assert_equal false, product_2.save
|
205
|
-
def any_instance
|
206
|
-
@any_instance ||= AnyInstance.new(self)
|
207
|
-
end
|
208
|
-
|
209
|
-
end
|
210
|
-
|
211
|
-
end
|
212
|
-
|
213
|
-
class Object # :nodoc:
|
214
|
-
include Mocha::ObjectMethods
|
215
|
-
end
|
216
|
-
|
217
|
-
class Module # :nodoc:
|
218
|
-
include Mocha::ModuleMethods
|
219
|
-
end
|
220
|
-
|
221
|
-
class Class # :nodoc:
|
222
|
-
include Mocha::ClassMethods
|
223
|
-
end
|
data/lib/mocha/options.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
$mocha_options = (ENV['MOCHA_OPTIONS'] || '').split(',').inject({}) { |hash, key| hash[key] = true; hash }
|
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'mocha/parameter_matchers/base'
|
2
|
-
require 'uri'
|
3
|
-
|
4
|
-
module Mocha
|
5
|
-
module ParameterMatchers
|
6
|
-
|
7
|
-
# :call-seq: has_equivalent_query_string(uri) -> parameter_matcher
|
8
|
-
#
|
9
|
-
# Matches a URI without regard to the ordering of parameters in the query string
|
10
|
-
# object = mock()
|
11
|
-
# object.expects(:method_1).with(has_equivalent_query_string('http://example.com/foo?a=1&b=2))
|
12
|
-
# object.method_1('http://example.com/foo?b=2&a=1')
|
13
|
-
# # no error raised
|
14
|
-
#
|
15
|
-
# object = mock()
|
16
|
-
# object.expects(:method_1).with(has_equivalent_query_string('http://example.com/foo?a=1&b=2))
|
17
|
-
# object.method_1('http://example.com/foo?a=1&b=3')
|
18
|
-
# # error raised, because the query parameters were different
|
19
|
-
def has_equivalent_query_string(uri)
|
20
|
-
QueryStringMatches.new(uri)
|
21
|
-
end
|
22
|
-
|
23
|
-
class QueryStringMatches < Base # :nodoc:
|
24
|
-
|
25
|
-
def initialize(uri)
|
26
|
-
@uri = URI.parse(uri)
|
27
|
-
end
|
28
|
-
|
29
|
-
def matches?(available_parameters)
|
30
|
-
actual = explode(URI.parse(available_parameters.shift))
|
31
|
-
expected = explode(@uri)
|
32
|
-
actual == expected
|
33
|
-
end
|
34
|
-
|
35
|
-
def mocha_inspect
|
36
|
-
"has_equivalent_query_string(#{@uri.mocha_inspect})"
|
37
|
-
end
|
38
|
-
|
39
|
-
private
|
40
|
-
def explode(uri)
|
41
|
-
query_hash = (uri.query || '').split('&').inject({}){ |h, kv| h.merge(Hash[*kv.split('=')]) }
|
42
|
-
URI::Generic::COMPONENT.inject({}){ |h, k| h.merge(k => uri.__send__(k)) }.merge(:query => query_hash)
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
require 'mocha/inspect'
|
2
|
-
|
3
|
-
module Mocha
|
4
|
-
|
5
|
-
class PrettyParameters
|
6
|
-
|
7
|
-
def initialize(params)
|
8
|
-
@params = params
|
9
|
-
@params_string = params.mocha_inspect
|
10
|
-
end
|
11
|
-
|
12
|
-
def pretty
|
13
|
-
remove_outer_array_braces!
|
14
|
-
remove_outer_hash_braces!
|
15
|
-
@params_string
|
16
|
-
end
|
17
|
-
|
18
|
-
def remove_outer_array_braces!
|
19
|
-
@params_string = @params_string.gsub(/^\[|\]$/, '')
|
20
|
-
end
|
21
|
-
|
22
|
-
def remove_outer_hash_braces!
|
23
|
-
@params_string = @params_string.gsub(/^\{|\}$/, '') if @params.length == 1
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
data/lib/mocha/single_yield.rb
DELETED
data/lib/mocha/standalone.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'mocha/api'
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module Mocha # :nodoc:
|
2
|
-
|
3
|
-
class UnexpectedInvocation
|
4
|
-
|
5
|
-
def initialize(mock, symbol, *arguments)
|
6
|
-
@mock = mock
|
7
|
-
@method_matcher = MethodMatcher.new(symbol)
|
8
|
-
@parameters_matcher = ParametersMatcher.new(arguments)
|
9
|
-
end
|
10
|
-
|
11
|
-
def to_s
|
12
|
-
method_signature = "#{@mock.mocha_inspect}.#{@method_matcher.mocha_inspect}#{@parameters_matcher.mocha_inspect}"
|
13
|
-
"unexpected invocation: #{method_signature}\n"
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
data/lib/mocha_standalone.rb
DELETED
data/lib/stubba.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
require File.expand_path('../../test_helper', __FILE__)
|
2
|
-
require 'test_runner'
|
3
|
-
require 'mocha/configuration'
|
4
|
-
require 'introspection'
|
5
|
-
|
6
|
-
module AcceptanceTest
|
7
|
-
|
8
|
-
class FakeLogger
|
9
|
-
|
10
|
-
attr_reader :warnings
|
11
|
-
|
12
|
-
def initialize
|
13
|
-
@warnings = []
|
14
|
-
end
|
15
|
-
|
16
|
-
def warn(message)
|
17
|
-
@warnings << message
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
attr_reader :logger
|
23
|
-
|
24
|
-
include TestRunner
|
25
|
-
|
26
|
-
def setup_acceptance_test
|
27
|
-
Mocha::Configuration.reset_configuration
|
28
|
-
@logger = FakeLogger.new
|
29
|
-
mockery = Mocha::Mockery.instance
|
30
|
-
@original_logger = mockery.logger
|
31
|
-
mockery.logger = @logger
|
32
|
-
end
|
33
|
-
|
34
|
-
def teardown_acceptance_test
|
35
|
-
Mocha::Configuration.reset_configuration
|
36
|
-
Mocha::Mockery.instance.logger = @original_logger
|
37
|
-
end
|
38
|
-
|
39
|
-
include Introspection::Assertions
|
40
|
-
|
41
|
-
end
|
data/test/acceptance/api_test.rb
DELETED
@@ -1,139 +0,0 @@
|
|
1
|
-
require File.expand_path('../acceptance_test_helper', __FILE__)
|
2
|
-
require 'mocha_standalone'
|
3
|
-
require 'simple_counter'
|
4
|
-
|
5
|
-
class NotATestUnitAssertionFailedError < StandardError
|
6
|
-
end
|
7
|
-
|
8
|
-
class NotATestUnitTestCase
|
9
|
-
|
10
|
-
include Mocha::API
|
11
|
-
|
12
|
-
attr_reader :assertion_counter
|
13
|
-
|
14
|
-
def initialize
|
15
|
-
@assertion_counter = SimpleCounter.new
|
16
|
-
end
|
17
|
-
|
18
|
-
def run(test_method)
|
19
|
-
mocha_setup
|
20
|
-
begin
|
21
|
-
prepare
|
22
|
-
begin
|
23
|
-
send(test_method)
|
24
|
-
mocha_verify(@assertion_counter)
|
25
|
-
rescue Mocha::ExpectationError => e
|
26
|
-
new_error = NotATestUnitAssertionFailedError.new(e.message)
|
27
|
-
new_error.set_backtrace(e.backtrace)
|
28
|
-
raise new_error
|
29
|
-
ensure
|
30
|
-
cleanup
|
31
|
-
end
|
32
|
-
ensure
|
33
|
-
mocha_teardown
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def prepare
|
38
|
-
end
|
39
|
-
|
40
|
-
def cleanup
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
class SampleTest < NotATestUnitTestCase
|
46
|
-
|
47
|
-
def mocha_with_fulfilled_expectation
|
48
|
-
mockee = mock()
|
49
|
-
mockee.expects(:blah)
|
50
|
-
mockee.blah
|
51
|
-
end
|
52
|
-
|
53
|
-
def mocha_with_unfulfilled_expectation
|
54
|
-
mockee = mock()
|
55
|
-
mockee.expects(:blah)
|
56
|
-
end
|
57
|
-
|
58
|
-
def mocha_with_unexpected_invocation
|
59
|
-
mockee = mock()
|
60
|
-
mockee.blah
|
61
|
-
end
|
62
|
-
|
63
|
-
def stubba_with_fulfilled_expectation
|
64
|
-
stubbee = Class.new { define_method(:blah) {} }.new
|
65
|
-
stubbee.expects(:blah)
|
66
|
-
stubbee.blah
|
67
|
-
end
|
68
|
-
|
69
|
-
def stubba_with_unfulfilled_expectation
|
70
|
-
stubbee = Class.new { define_method(:blah) {} }.new
|
71
|
-
stubbee.expects(:blah)
|
72
|
-
end
|
73
|
-
|
74
|
-
def mocha_with_matching_parameter
|
75
|
-
mockee = mock()
|
76
|
-
mockee.expects(:blah).with(has_key(:wibble))
|
77
|
-
mockee.blah(:wibble => 1)
|
78
|
-
end
|
79
|
-
|
80
|
-
def mocha_with_non_matching_parameter
|
81
|
-
mockee = mock()
|
82
|
-
mockee.expects(:blah).with(has_key(:wibble))
|
83
|
-
mockee.blah(:wobble => 2)
|
84
|
-
end
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
require 'test/unit'
|
89
|
-
|
90
|
-
class APITest < Test::Unit::TestCase
|
91
|
-
|
92
|
-
attr_reader :sample_test
|
93
|
-
|
94
|
-
include AcceptanceTest
|
95
|
-
|
96
|
-
def setup
|
97
|
-
@sample_test = SampleTest.new
|
98
|
-
setup_acceptance_test
|
99
|
-
end
|
100
|
-
|
101
|
-
def teardown
|
102
|
-
teardown_acceptance_test
|
103
|
-
end
|
104
|
-
|
105
|
-
def test_should_pass_mocha_test
|
106
|
-
assert_nothing_raised { sample_test.run(:mocha_with_fulfilled_expectation) }
|
107
|
-
assert_equal 1, sample_test.assertion_counter.count
|
108
|
-
end
|
109
|
-
|
110
|
-
def test_should_fail_mocha_test_due_to_unfulfilled_exception
|
111
|
-
assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:mocha_with_unfulfilled_expectation) }
|
112
|
-
assert_equal 1, sample_test.assertion_counter.count
|
113
|
-
end
|
114
|
-
|
115
|
-
def test_should_fail_mocha_test_due_to_unexpected_invocation
|
116
|
-
assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:mocha_with_unexpected_invocation) }
|
117
|
-
assert_equal 0, sample_test.assertion_counter.count
|
118
|
-
end
|
119
|
-
|
120
|
-
def test_should_pass_stubba_test
|
121
|
-
assert_nothing_raised { sample_test.run(:stubba_with_fulfilled_expectation) }
|
122
|
-
assert_equal 1, sample_test.assertion_counter.count
|
123
|
-
end
|
124
|
-
|
125
|
-
def test_should_fail_stubba_test
|
126
|
-
assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:stubba_with_unfulfilled_expectation) }
|
127
|
-
assert_equal 1, sample_test.assertion_counter.count
|
128
|
-
end
|
129
|
-
|
130
|
-
def test_should_pass_mocha_test_with_matching_parameter
|
131
|
-
assert_nothing_raised { sample_test.run(:mocha_with_matching_parameter) }
|
132
|
-
assert_equal 1, sample_test.assertion_counter.count
|
133
|
-
end
|
134
|
-
|
135
|
-
def test_should_fail_mocha_test_with_non_matching_parameter
|
136
|
-
assert_raises(NotATestUnitAssertionFailedError) { sample_test.run(:mocha_with_non_matching_parameter) }
|
137
|
-
end
|
138
|
-
|
139
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
require File.expand_path('../acceptance_test_helper', __FILE__)
|
2
|
-
require 'mocha'
|
3
|
-
|
4
|
-
class Bug18914Test < Test::Unit::TestCase
|
5
|
-
|
6
|
-
include AcceptanceTest
|
7
|
-
|
8
|
-
def setup
|
9
|
-
setup_acceptance_test
|
10
|
-
end
|
11
|
-
|
12
|
-
def teardown
|
13
|
-
teardown_acceptance_test
|
14
|
-
end
|
15
|
-
|
16
|
-
class AlwaysEql
|
17
|
-
|
18
|
-
def my_method
|
19
|
-
true
|
20
|
-
end
|
21
|
-
|
22
|
-
def ==(o)
|
23
|
-
true
|
24
|
-
end
|
25
|
-
|
26
|
-
def eql?(o)
|
27
|
-
true
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_should_not_allow_stubbing_of_non_mock_instance_disrupted_by_legitimate_overriding_of_eql_method
|
33
|
-
|
34
|
-
always_eql_1 = AlwaysEql.new
|
35
|
-
always_eql_1.stubs(:my_method).returns(false)
|
36
|
-
|
37
|
-
always_eql_2 = AlwaysEql.new
|
38
|
-
always_eql_2.stubs(:my_method).returns(false)
|
39
|
-
|
40
|
-
assert_equal false, always_eql_2.my_method
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require File.expand_path('../acceptance_test_helper', __FILE__)
|
2
|
-
require 'mocha'
|
3
|
-
|
4
|
-
class Bug21465Test < Test::Unit::TestCase
|
5
|
-
|
6
|
-
include AcceptanceTest
|
7
|
-
|
8
|
-
def setup
|
9
|
-
setup_acceptance_test
|
10
|
-
end
|
11
|
-
|
12
|
-
def teardown
|
13
|
-
teardown_acceptance_test
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_should_allow_expected_method_name_to_be_a_string
|
17
|
-
test_result = run_as_test do
|
18
|
-
mock = mock()
|
19
|
-
mock.expects('wibble')
|
20
|
-
mock.wibble
|
21
|
-
end
|
22
|
-
assert_passed(test_result)
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_should_allow_stubbed_method_name_to_be_a_string
|
26
|
-
test_result = run_as_test do
|
27
|
-
mock = mock()
|
28
|
-
mock.stubs('wibble')
|
29
|
-
mock.wibble
|
30
|
-
end
|
31
|
-
assert_passed(test_result)
|
32
|
-
end
|
33
|
-
|
34
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
require File.expand_path('../acceptance_test_helper', __FILE__)
|
2
|
-
require 'mocha'
|
3
|
-
|
4
|
-
class Bug21563Test < Test::Unit::TestCase
|
5
|
-
|
6
|
-
include AcceptanceTest
|
7
|
-
|
8
|
-
def setup
|
9
|
-
setup_acceptance_test
|
10
|
-
end
|
11
|
-
|
12
|
-
def teardown
|
13
|
-
teardown_acceptance_test
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_should_allow_stubbing_of_verified_method
|
17
|
-
test_result = run_as_test do
|
18
|
-
object = Object.new
|
19
|
-
object.stubs(:verified?).returns(false)
|
20
|
-
assert !object.verified?
|
21
|
-
end
|
22
|
-
assert_passed(test_result)
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|