mocha 0.10.5 → 0.11.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.
- data/COPYING.rdoc +1 -1
- data/README.rdoc +6 -4
- data/RELEASE.rdoc +12 -0
- data/Rakefile +29 -60
- data/doc/Mocha.html +112 -0
- data/doc/Mocha/API.html +898 -0
- data/doc/Mocha/ClassMethods.html +246 -0
- data/doc/Mocha/Configuration.html +471 -0
- data/doc/Mocha/Expectation.html +2570 -0
- data/doc/Mocha/Mock.html +830 -0
- data/doc/Mocha/ObjectMethods.html +668 -0
- data/doc/Mocha/ParameterMatchers.html +2715 -0
- data/doc/Mocha/ParameterMatchers/AllOf.html +137 -0
- data/doc/Mocha/ParameterMatchers/AnyOf.html +137 -0
- data/doc/Mocha/ParameterMatchers/AnyParameters.html +136 -0
- data/doc/Mocha/ParameterMatchers/Anything.html +136 -0
- data/doc/Mocha/ParameterMatchers/Base.html +419 -0
- data/doc/Mocha/ParameterMatchers/Equals.html +137 -0
- data/doc/Mocha/ParameterMatchers/HasEntries.html +137 -0
- data/doc/Mocha/ParameterMatchers/HasEntry.html +137 -0
- data/doc/Mocha/ParameterMatchers/HasKey.html +137 -0
- data/doc/Mocha/ParameterMatchers/HasValue.html +137 -0
- data/doc/Mocha/ParameterMatchers/Includes.html +137 -0
- data/doc/Mocha/ParameterMatchers/InstanceOf.html +137 -0
- data/doc/Mocha/ParameterMatchers/IsA.html +136 -0
- data/doc/Mocha/ParameterMatchers/KindOf.html +137 -0
- data/doc/Mocha/ParameterMatchers/Not.html +137 -0
- data/doc/Mocha/ParameterMatchers/Optionally.html +136 -0
- data/doc/Mocha/ParameterMatchers/QueryStringMatches.html +136 -0
- data/doc/Mocha/ParameterMatchers/RegexpMatches.html +137 -0
- data/doc/Mocha/ParameterMatchers/RespondsWith.html +137 -0
- data/doc/Mocha/ParameterMatchers/YamlEquivalent.html +137 -0
- data/doc/Mocha/Sequence.html +133 -0
- data/doc/Mocha/StateMachine.html +510 -0
- data/doc/Mocha/StateMachine/State.html +125 -0
- data/doc/Mocha/StateMachine/StatePredicate.html +125 -0
- data/doc/Mocha/StubbingError.html +134 -0
- data/doc/Mocha/UnexpectedInvocation.html +124 -0
- data/doc/_index.html +481 -0
- data/doc/class_list.html +47 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +55 -0
- data/doc/css/style.css +322 -0
- data/doc/file.COPYING.html +72 -0
- data/doc/file.MIT-LICENSE.html +86 -0
- data/doc/file.README.html +153 -0
- data/doc/file.RELEASE.html +933 -0
- data/doc/file.misc.html +108 -0
- data/doc/file.mocha.html +90 -0
- data/doc/file.stubba.html +129 -0
- data/doc/file_list.html +67 -0
- data/doc/frames.html +13 -0
- data/doc/index.html +153 -0
- data/doc/js/app.js +205 -0
- data/doc/js/full_list.js +173 -0
- data/doc/js/jquery.js +16 -0
- data/doc/method_list.html +510 -0
- data/doc/top-level-namespace.html +105 -0
- data/lib/mocha/any_instance_method.rb +16 -11
- data/lib/mocha/api.rb +120 -106
- data/lib/mocha/argument_iterator.rb +6 -6
- data/lib/mocha/backtrace_filter.rb +1 -1
- data/lib/mocha/cardinality.rb +21 -21
- data/lib/mocha/central.rb +8 -8
- data/lib/mocha/change_state_side_effect.rb +5 -5
- data/lib/mocha/class_method.rb +14 -19
- data/lib/mocha/configuration.rb +47 -37
- data/lib/mocha/deprecation.rb +8 -8
- data/lib/mocha/exception_raiser.rb +7 -7
- data/lib/mocha/expectation.rb +173 -77
- data/lib/mocha/expectation_error.rb +2 -2
- data/lib/mocha/expectation_list.rb +11 -11
- data/lib/mocha/in_state_ordering_constraint.rb +5 -5
- data/lib/mocha/inspect.rb +8 -8
- data/lib/mocha/instance_method.rb +1 -17
- data/lib/mocha/integration.rb +9 -9
- data/lib/mocha/integration/mini_test/assertion_counter.rb +9 -9
- data/lib/mocha/integration/mini_test/exception_translation.rb +6 -6
- data/lib/mocha/integration/mini_test/version_13.rb +6 -6
- data/lib/mocha/integration/mini_test/version_140.rb +6 -6
- data/lib/mocha/integration/mini_test/version_141.rb +6 -6
- data/lib/mocha/integration/mini_test/version_142_to_172.rb +7 -7
- data/lib/mocha/integration/mini_test/version_200.rb +7 -7
- data/lib/mocha/integration/test_unit/assertion_counter.rb +10 -10
- data/lib/mocha/integration/test_unit/gem_version_200.rb +6 -6
- data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +6 -6
- data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +6 -6
- data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +6 -6
- data/lib/mocha/is_a.rb +1 -1
- data/lib/mocha/logger.rb +6 -6
- data/lib/mocha/method_matcher.rb +7 -7
- data/lib/mocha/mock.rb +85 -48
- data/lib/mocha/mockery.rb +47 -33
- data/lib/mocha/module_method.rb +2 -2
- data/lib/mocha/multiple_yields.rb +9 -9
- data/lib/mocha/names.rb +18 -18
- data/lib/mocha/no_yields.rb +7 -7
- data/lib/mocha/object.rb +104 -75
- data/lib/mocha/parameter_matchers.rb +3 -3
- data/lib/mocha/parameter_matchers/all_of.rb +22 -12
- data/lib/mocha/parameter_matchers/any_of.rb +23 -12
- data/lib/mocha/parameter_matchers/any_parameters.rb +15 -8
- data/lib/mocha/parameter_matchers/anything.rb +20 -11
- data/lib/mocha/parameter_matchers/base.rb +35 -27
- data/lib/mocha/parameter_matchers/equals.rb +23 -12
- data/lib/mocha/parameter_matchers/has_entries.rb +22 -12
- data/lib/mocha/parameter_matchers/has_entry.rb +31 -13
- data/lib/mocha/parameter_matchers/has_key.rb +21 -11
- data/lib/mocha/parameter_matchers/has_value.rb +21 -11
- data/lib/mocha/parameter_matchers/includes.rb +15 -5
- data/lib/mocha/parameter_matchers/instance_of.rb +23 -12
- data/lib/mocha/parameter_matchers/is_a.rb +23 -12
- data/lib/mocha/parameter_matchers/kind_of.rb +23 -12
- data/lib/mocha/parameter_matchers/not.rb +22 -12
- data/lib/mocha/parameter_matchers/object.rb +5 -3
- data/lib/mocha/parameter_matchers/optionally.rb +24 -12
- data/lib/mocha/parameter_matchers/query_string.rb +14 -3
- data/lib/mocha/parameter_matchers/regexp_matches.rb +21 -11
- data/lib/mocha/parameter_matchers/responds_with.rb +17 -6
- data/lib/mocha/parameter_matchers/yaml_equivalent.rb +16 -6
- data/lib/mocha/parameters_matcher.rb +8 -8
- data/lib/mocha/pretty_parameters.rb +7 -7
- data/lib/mocha/return_values.rb +11 -11
- data/lib/mocha/sequence.rb +23 -14
- data/lib/mocha/single_return_value.rb +7 -7
- data/lib/mocha/single_yield.rb +9 -9
- data/lib/mocha/standalone.rb +1 -1
- data/lib/mocha/state_machine.rb +61 -46
- data/lib/mocha/stubbing_error.rb +8 -5
- data/lib/mocha/thrower.rb +2 -2
- data/lib/mocha/unexpected_invocation.rb +9 -6
- data/lib/mocha/version.rb +1 -1
- data/lib/mocha/yield_parameters.rb +10 -10
- data/lib/mocha_standalone.rb +1 -1
- data/mocha.gemspec +4 -7
- data/test/acceptance/acceptance_test_helper.rb +10 -10
- data/test/acceptance/api_test.rb +20 -20
- data/test/acceptance/bug_18914_test.rb +12 -12
- data/test/acceptance/bug_21465_test.rb +6 -6
- data/test/acceptance/bug_21563_test.rb +5 -5
- data/test/acceptance/exception_rescue_test.rb +1 -1
- data/test/acceptance/expected_invocation_count_test.rb +17 -17
- data/test/acceptance/failure_messages_test.rb +13 -13
- data/test/acceptance/minitest_test.rb +39 -39
- data/test/acceptance/mocha_example_test.rb +26 -26
- data/test/acceptance/mocha_test_result_test.rb +13 -13
- data/test/acceptance/mock_test.rb +5 -5
- data/test/acceptance/mock_with_initializer_block_test.rb +5 -5
- data/test/acceptance/mocked_methods_dispatch_test.rb +4 -4
- data/test/acceptance/multiple_expectations_failure_message_test.rb +2 -2
- data/test/acceptance/optional_parameters_test.rb +5 -5
- data/test/acceptance/parameter_matcher_test.rb +18 -18
- data/test/acceptance/partial_mocks_test.rb +9 -9
- data/test/acceptance/raise_exception_test.rb +1 -1
- data/test/acceptance/return_value_test.rb +5 -5
- data/test/acceptance/sequence_test.rb +29 -29
- data/test/acceptance/states_test.rb +17 -17
- data/test/acceptance/stub_any_instance_method_test.rb +13 -13
- data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +7 -7
- data/test/acceptance/stub_everything_test.rb +5 -5
- data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +1 -1
- data/test/acceptance/stub_module_method_test.rb +17 -17
- data/test/acceptance/stub_test.rb +5 -5
- data/test/acceptance/stubba_example_test.rb +24 -24
- data/test/acceptance/stubba_test_result_test.rb +11 -11
- data/test/acceptance/stubbing_error_backtrace_test.rb +6 -6
- data/test/acceptance/stubbing_frozen_object_test.rb +88 -0
- data/test/acceptance/stubbing_method_unnecessarily_test.rb +9 -9
- data/test/acceptance/stubbing_nil_test.rb +59 -0
- data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +13 -13
- data/test/acceptance/stubbing_non_existent_class_method_test.rb +15 -15
- data/test/acceptance/stubbing_non_existent_instance_method_test.rb +14 -14
- data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +13 -13
- data/test/acceptance/stubbing_non_public_class_method_test.rb +12 -12
- data/test/acceptance/stubbing_non_public_instance_method_test.rb +12 -12
- data/test/acceptance/stubbing_on_non_mock_object_test.rb +9 -9
- data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +35 -0
- data/test/acceptance/throw_test.rb +1 -1
- data/test/deprecation_disabler.rb +1 -1
- data/test/execution_point.rb +7 -7
- data/test/method_definer.rb +1 -1
- data/test/mini_test_result.rb +21 -21
- data/test/simple_counter.rb +5 -5
- data/test/test_helper.rb +1 -1
- data/test/test_runner.rb +22 -24
- data/test/test_unit_result.rb +20 -0
- data/test/unit/any_instance_method_test.rb +28 -32
- data/test/unit/array_inspect_test.rb +3 -3
- data/test/unit/backtrace_filter_test.rb +3 -3
- data/test/unit/cardinality_test.rb +12 -12
- data/test/unit/central_test.rb +18 -18
- data/test/unit/change_state_side_effect_test.rb +12 -12
- data/test/unit/class_method_test.rb +33 -91
- data/test/unit/date_time_inspect_test.rb +4 -4
- data/test/unit/exception_raiser_test.rb +5 -5
- data/test/unit/expectation_list_test.rb +7 -7
- data/test/unit/expectation_test.rb +66 -66
- data/test/unit/hash_inspect_test.rb +4 -4
- data/test/unit/in_state_ordering_constraint_test.rb +13 -13
- data/test/unit/method_matcher_test.rb +3 -3
- data/test/unit/mock_test.rb +40 -40
- data/test/unit/mockery_test.rb +25 -25
- data/test/unit/multiple_yields_test.rb +2 -2
- data/test/unit/no_yields_test.rb +2 -2
- data/test/unit/object_inspect_test.rb +4 -4
- data/test/unit/object_test.rb +15 -15
- data/test/unit/parameter_matchers/all_of_test.rb +6 -6
- data/test/unit/parameter_matchers/any_of_test.rb +6 -6
- data/test/unit/parameter_matchers/anything_test.rb +5 -5
- data/test/unit/parameter_matchers/has_entries_test.rb +10 -10
- data/test/unit/parameter_matchers/has_entry_test.rb +13 -13
- data/test/unit/parameter_matchers/has_key_test.rb +11 -11
- data/test/unit/parameter_matchers/has_value_test.rb +12 -12
- data/test/unit/parameter_matchers/includes_test.rb +4 -4
- data/test/unit/parameter_matchers/not_test.rb +6 -6
- data/test/unit/parameter_matchers/regexp_matches_test.rb +9 -9
- data/test/unit/parameter_matchers/responds_with_test.rb +6 -6
- data/test/unit/parameter_matchers/stub_matcher.rb +4 -4
- data/test/unit/parameter_matchers/yaml_equivalent_test.rb +6 -6
- data/test/unit/parameters_matcher_test.rb +16 -16
- data/test/unit/return_values_test.rb +5 -5
- data/test/unit/sequence_test.rb +10 -10
- data/test/unit/single_return_value_test.rb +3 -3
- data/test/unit/single_yield_test.rb +2 -2
- data/test/unit/state_machine_test.rb +19 -19
- data/test/unit/string_inspect_test.rb +2 -2
- data/test/unit/thrower_test.rb +1 -1
- data/test/unit/yield_parameters_test.rb +11 -11
- metadata +113 -101
@@ -1,23 +1,23 @@
|
|
1
1
|
module Mocha
|
2
|
-
|
2
|
+
|
3
3
|
module Integration
|
4
|
-
|
4
|
+
|
5
5
|
module TestUnit
|
6
|
-
|
6
|
+
|
7
7
|
class AssertionCounter
|
8
|
-
|
8
|
+
|
9
9
|
def initialize(test_result)
|
10
10
|
@test_result = test_result
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def increment
|
14
14
|
@test_result.add_assertion
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
end
|
22
|
-
|
23
|
-
end
|
22
|
+
|
23
|
+
end
|
@@ -3,11 +3,11 @@ require 'mocha/integration/test_unit/assertion_counter'
|
|
3
3
|
require 'mocha/expectation_error'
|
4
4
|
|
5
5
|
module Mocha
|
6
|
-
|
6
|
+
|
7
7
|
module Integration
|
8
|
-
|
8
|
+
|
9
9
|
module TestUnit
|
10
|
-
|
10
|
+
|
11
11
|
module GemVersion200
|
12
12
|
def self.included(mod)
|
13
13
|
$stderr.puts "Monkey patching Test::Unit gem v2.0.0" if $mocha_options['debug']
|
@@ -44,9 +44,9 @@ module Mocha
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
end
|
@@ -3,11 +3,11 @@ require 'mocha/integration/test_unit/assertion_counter'
|
|
3
3
|
require 'mocha/expectation_error'
|
4
4
|
|
5
5
|
module Mocha
|
6
|
-
|
6
|
+
|
7
7
|
module Integration
|
8
|
-
|
8
|
+
|
9
9
|
module TestUnit
|
10
|
-
|
10
|
+
|
11
11
|
module GemVersion201To202
|
12
12
|
def self.included(mod)
|
13
13
|
$stderr.puts "Monkey patching Test::Unit gem >= v2.0.1 and <= v2.0.2" if $mocha_options['debug']
|
@@ -44,9 +44,9 @@ module Mocha
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
end
|
@@ -3,11 +3,11 @@ require 'mocha/integration/test_unit/assertion_counter'
|
|
3
3
|
require 'mocha/expectation_error'
|
4
4
|
|
5
5
|
module Mocha
|
6
|
-
|
6
|
+
|
7
7
|
module Integration
|
8
|
-
|
8
|
+
|
9
9
|
module TestUnit
|
10
|
-
|
10
|
+
|
11
11
|
module RubyVersion185AndBelow
|
12
12
|
def self.included(mod)
|
13
13
|
$stderr.puts "Monkey patching Test::Unit for Ruby <= v1.8.5" if $mocha_options['debug']
|
@@ -43,9 +43,9 @@ module Mocha
|
|
43
43
|
yield(Test::Unit::TestCase::FINISHED, name)
|
44
44
|
end
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
end
|
@@ -3,11 +3,11 @@ require 'mocha/integration/test_unit/assertion_counter'
|
|
3
3
|
require 'mocha/expectation_error'
|
4
4
|
|
5
5
|
module Mocha
|
6
|
-
|
6
|
+
|
7
7
|
module Integration
|
8
|
-
|
8
|
+
|
9
9
|
module TestUnit
|
10
|
-
|
10
|
+
|
11
11
|
module RubyVersion186AndAbove
|
12
12
|
def self.included(mod)
|
13
13
|
$stderr.puts "Monkey patching Test::Unit for Ruby >= v1.8.6" if $mocha_options['debug']
|
@@ -45,9 +45,9 @@ module Mocha
|
|
45
45
|
yield(Test::Unit::TestCase::FINISHED, name)
|
46
46
|
end
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
end
|
data/lib/mocha/is_a.rb
CHANGED
data/lib/mocha/logger.rb
CHANGED
data/lib/mocha/method_matcher.rb
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
module Mocha
|
2
|
-
|
2
|
+
|
3
3
|
class MethodMatcher
|
4
|
-
|
4
|
+
|
5
5
|
attr_reader :expected_method_name
|
6
|
-
|
6
|
+
|
7
7
|
def initialize(expected_method_name)
|
8
8
|
@expected_method_name = expected_method_name
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def match?(actual_method_name)
|
12
12
|
@expected_method_name == actual_method_name
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def mocha_inspect
|
16
16
|
"#{@expected_method_name}"
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
end
|
20
20
|
|
21
|
-
end
|
21
|
+
end
|
data/lib/mocha/mock.rb
CHANGED
@@ -7,37 +7,47 @@ require 'mocha/parameters_matcher'
|
|
7
7
|
require 'mocha/unexpected_invocation'
|
8
8
|
require 'mocha/argument_iterator'
|
9
9
|
|
10
|
-
module Mocha
|
10
|
+
module Mocha
|
11
11
|
|
12
12
|
# Traditional mock object.
|
13
13
|
#
|
14
|
-
#
|
14
|
+
# All methods return an {Expectation} which can be further modified by methods on {Expectation}.
|
15
15
|
class Mock
|
16
16
|
|
17
|
-
#
|
18
|
-
# expects(method_names_vs_return_values) -> last expectation
|
17
|
+
# Adds an expectation that the specified method must be called exactly once with any parameters.
|
19
18
|
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
# object = mock()
|
23
|
-
# object.expects(:method1)
|
24
|
-
# object.method1
|
25
|
-
# # no error raised
|
19
|
+
# @param [Symbol,String] method_name name of expected method
|
20
|
+
# @param [Hash] expected_methods_vs_return_values expected method name symbols as keys and corresponding return values as values - these expectations are setup as if {#expects} were called multiple times.
|
26
21
|
#
|
22
|
+
# @overload def expects(method_name)
|
23
|
+
# @overload def expects(expected_methods_vs_return_values)
|
24
|
+
# @return [Expectation] last-built expectation which can be further modified by methods on {Expectation}.
|
25
|
+
#
|
26
|
+
# @example Expected method invoked once so no error raised
|
27
27
|
# object = mock()
|
28
|
-
# object.expects(:
|
29
|
-
#
|
30
|
-
#
|
28
|
+
# object.expects(:expected_method)
|
29
|
+
# object.expected_method
|
30
|
+
#
|
31
|
+
# @example Expected method not invoked so error raised
|
31
32
|
# object = mock()
|
32
|
-
# object.expects(:
|
33
|
+
# object.expects(:expected_method)
|
34
|
+
# # error raised when test completes, because expected_method not called exactly once
|
33
35
|
#
|
34
|
-
#
|
36
|
+
# @example Expected method invoked twice so error raised
|
37
|
+
# object = mock()
|
38
|
+
# object.expects(:expected_method)
|
39
|
+
# object.expected_method
|
40
|
+
# object.expected_method # => error raised when expected method invoked second time
|
35
41
|
#
|
42
|
+
# @example Setup multiple expectations using +expected_methods_vs_return_values+.
|
36
43
|
# object = mock()
|
37
|
-
# object.expects(:
|
38
|
-
#
|
44
|
+
# object.expects(:expected_method_one => :result_one, :expected_method_two => :result_two)
|
45
|
+
#
|
46
|
+
# # is exactly equivalent to
|
39
47
|
#
|
40
|
-
#
|
48
|
+
# object = mock()
|
49
|
+
# object.expects(:expected_method_one).returns(:result_one)
|
50
|
+
# object.expects(:expected_method_two).returns(:result_two)
|
41
51
|
def expects(method_name_or_hash, backtrace = nil)
|
42
52
|
iterator = ArgumentIterator.new(method_name_or_hash)
|
43
53
|
iterator.each { |*args|
|
@@ -49,27 +59,31 @@ module Mocha # :nodoc:
|
|
49
59
|
}
|
50
60
|
end
|
51
61
|
|
52
|
-
#
|
53
|
-
# stubs(method_names_vs_return_values) -> last expectation
|
62
|
+
# Adds an expectation that the specified method may be called any number of times with any parameters.
|
54
63
|
#
|
55
|
-
#
|
56
|
-
#
|
64
|
+
# @param [Symbol,String] method_name name of stubbed method
|
65
|
+
# @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {#stubs} were called multiple times.
|
66
|
+
#
|
67
|
+
# @overload def stubs(method_name)
|
68
|
+
# @overload def stubs(stubbed_methods_vs_return_values)
|
69
|
+
# @return [Expectation] last-built expectation which can be further modified by methods on {Expectation}.
|
70
|
+
#
|
71
|
+
# @example No error raised however many times stubbed method is invoked
|
57
72
|
# object = mock()
|
58
|
-
# object.stubs(:
|
59
|
-
# object.
|
60
|
-
# object.
|
73
|
+
# object.stubs(:stubbed_method)
|
74
|
+
# object.stubbed_method
|
75
|
+
# object.stubbed_method
|
61
76
|
# # no error raised
|
62
|
-
#
|
77
|
+
#
|
78
|
+
# @example Setup multiple expectations using +stubbed_methods_vs_return_values+.
|
63
79
|
# object = mock()
|
64
|
-
# object.stubs(:
|
80
|
+
# object.stubs(:stubbed_method_one => :result_one, :stubbed_method_two => :result_two)
|
65
81
|
#
|
66
|
-
# # exactly equivalent to
|
82
|
+
# # is exactly equivalent to
|
67
83
|
#
|
68
84
|
# object = mock()
|
69
|
-
# object.stubs(:
|
70
|
-
# object.stubs(:
|
71
|
-
#
|
72
|
-
# Aliased by <tt>\_\_stubs\_\_</tt>
|
85
|
+
# object.stubs(:stubbed_method_one).returns(:result_one)
|
86
|
+
# object.stubs(:stubbed_method_two).returns(:result_two)
|
73
87
|
def stubs(method_name_or_hash, backtrace = nil)
|
74
88
|
iterator = ArgumentIterator.new(method_name_or_hash)
|
75
89
|
iterator.each { |*args|
|
@@ -82,22 +96,30 @@ module Mocha # :nodoc:
|
|
82
96
|
}
|
83
97
|
end
|
84
98
|
|
99
|
+
# Removes the specified stubbed method (added by calls to {#expects} or {#stubs}) and all expectations associated with it.
|
100
|
+
#
|
101
|
+
# @param [Symbol] method_name name of method to unstub.
|
102
|
+
#
|
103
|
+
# @example Invoking an unstubbed method causes error to be raised
|
104
|
+
# object = mock('mock') do
|
105
|
+
# object.stubs(:stubbed_method).returns(:result_one)
|
106
|
+
# object.stubbed_method # => :result_one
|
107
|
+
# object.unstub(:stubbed_method)
|
108
|
+
# object.stubbed_method # => unexpected invocation: #<Mock:mock>.stubbed_method()
|
85
109
|
def unstub(method_name)
|
86
110
|
@expectations.remove_all_matching_method(method_name)
|
87
111
|
end
|
88
112
|
|
89
|
-
#
|
113
|
+
# Constrains the {Mock} instance so that it can only expect or stub methods to which +responder+ responds. The constraint is only applied at method invocation time.
|
90
114
|
#
|
91
|
-
#
|
115
|
+
# A +NoMethodError+ will be raised if the +responder+ does not +#respond_to?+ a method invocation (even if the method has been expected or stubbed).
|
92
116
|
#
|
93
|
-
#
|
117
|
+
# The {Mock} instance will delegate its +#respond_to?+ method to the +responder+.
|
94
118
|
#
|
95
|
-
#
|
96
|
-
#
|
97
|
-
# def chew(grass); end
|
98
|
-
# def self.number_of_legs; end
|
99
|
-
# end
|
119
|
+
# @param [Object, #respond_to?] responder an object used to determine whether {Mock} instance should +#respond_to?+ to an invocation.
|
120
|
+
# @return [Mock] the same {Mock} instance, thereby allowing invocations of other {Mock} methods to be chained.
|
100
121
|
#
|
122
|
+
# @example Normal mocking
|
101
123
|
# sheep = mock('sheep')
|
102
124
|
# sheep.expects(:chew)
|
103
125
|
# sheep.expects(:foo)
|
@@ -107,6 +129,11 @@ module Mocha # :nodoc:
|
|
107
129
|
# sheep.foo
|
108
130
|
# # no error raised
|
109
131
|
#
|
132
|
+
# @example Using {#responds_like} with an instance method
|
133
|
+
# class Sheep
|
134
|
+
# def chew(grass); end
|
135
|
+
# end
|
136
|
+
#
|
110
137
|
# sheep = mock('sheep')
|
111
138
|
# sheep.responds_like(Sheep.new)
|
112
139
|
# sheep.expects(:chew)
|
@@ -116,6 +143,11 @@ module Mocha # :nodoc:
|
|
116
143
|
# sheep.chew
|
117
144
|
# sheep.foo # => raises NoMethodError exception
|
118
145
|
#
|
146
|
+
# @example Using {#responds_like} with a class method
|
147
|
+
# class Sheep
|
148
|
+
# def self.number_of_legs; end
|
149
|
+
# end
|
150
|
+
#
|
119
151
|
# sheep_class = mock('sheep_class')
|
120
152
|
# sheep_class.responds_like(Sheep)
|
121
153
|
# sheep_class.stubs(:number_of_legs).returns(4)
|
@@ -124,15 +156,12 @@ module Mocha # :nodoc:
|
|
124
156
|
# sheep_class.respond_to?(:foo) # => false
|
125
157
|
# assert_equal 4, sheep_class.number_of_legs
|
126
158
|
# sheep_class.foo # => raises NoMethodError exception
|
127
|
-
|
128
|
-
|
129
|
-
def responds_like(object)
|
130
|
-
@responder = object
|
159
|
+
def responds_like(responder)
|
160
|
+
@responder = responder
|
131
161
|
self
|
132
162
|
end
|
133
163
|
|
134
|
-
#
|
135
|
-
|
164
|
+
# @private
|
136
165
|
def initialize(mockery, name = nil, &block)
|
137
166
|
@mockery = mockery
|
138
167
|
@name = name || DefaultName.new(self)
|
@@ -142,6 +171,7 @@ module Mocha # :nodoc:
|
|
142
171
|
instance_eval(&block) if block
|
143
172
|
end
|
144
173
|
|
174
|
+
# @private
|
145
175
|
attr_reader :everything_stubbed
|
146
176
|
|
147
177
|
alias_method :__expects__, :expects
|
@@ -150,14 +180,17 @@ module Mocha # :nodoc:
|
|
150
180
|
|
151
181
|
alias_method :quacks_like, :responds_like
|
152
182
|
|
183
|
+
# @private
|
153
184
|
def __expectations__
|
154
185
|
@expectations
|
155
186
|
end
|
156
187
|
|
188
|
+
# @private
|
157
189
|
def stub_everything
|
158
190
|
@everything_stubbed = true
|
159
191
|
end
|
160
192
|
|
193
|
+
# @private
|
161
194
|
def method_missing(symbol, *arguments, &block)
|
162
195
|
if @responder and not @responder.respond_to?(symbol)
|
163
196
|
raise NoMethodError, "undefined method `#{symbol}' for #{self.mocha_inspect} which responds like #{@responder.mocha_inspect}"
|
@@ -174,6 +207,7 @@ module Mocha # :nodoc:
|
|
174
207
|
end
|
175
208
|
end
|
176
209
|
|
210
|
+
# @private
|
177
211
|
def respond_to?(symbol, include_private = false)
|
178
212
|
if @responder then
|
179
213
|
if @responder.method(:respond_to?).arity > 1
|
@@ -186,28 +220,31 @@ module Mocha # :nodoc:
|
|
186
220
|
end
|
187
221
|
end
|
188
222
|
|
223
|
+
# @private
|
189
224
|
def __verified__?(assertion_counter = nil)
|
190
225
|
@expectations.verified?(assertion_counter)
|
191
226
|
end
|
192
227
|
|
228
|
+
# @private
|
193
229
|
def mocha_inspect
|
194
230
|
@name.mocha_inspect
|
195
231
|
end
|
196
232
|
|
233
|
+
# @private
|
197
234
|
def inspect
|
198
235
|
mocha_inspect
|
199
236
|
end
|
200
237
|
|
238
|
+
# @private
|
201
239
|
def ensure_method_not_already_defined(method_name)
|
202
240
|
self.__metaclass__.send(:undef_method, method_name) if self.__metaclass__.method_defined?(method_name)
|
203
241
|
end
|
204
242
|
|
243
|
+
# @private
|
205
244
|
def any_expectations?
|
206
245
|
@expectations.any?
|
207
246
|
end
|
208
247
|
|
209
|
-
# :startdoc:
|
210
|
-
|
211
248
|
end
|
212
249
|
|
213
250
|
end
|