mocha 1.1.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/FUNDING.yml +1 -0
- data/.rubocop.yml +53 -0
- data/.rubocop_todo.yml +27 -0
- data/.yardopts +1 -1
- data/CONTRIBUTING.md +4 -9
- data/COPYING.md +2 -2
- data/Gemfile +27 -0
- data/MIT-LICENSE.md +1 -1
- data/README.md +120 -30
- data/RELEASE.md +393 -1
- data/Rakefile +61 -50
- data/gemfiles/Gemfile.minitest.latest +1 -0
- data/gemfiles/Gemfile.test-unit.latest +1 -0
- data/lib/mocha/any_instance_method.rb +9 -74
- data/lib/mocha/api.rb +85 -69
- data/lib/mocha/argument_iterator.rb +4 -8
- data/lib/mocha/backtrace_filter.rb +3 -7
- data/lib/mocha/block_matcher.rb +31 -0
- data/lib/mocha/cardinality.rb +60 -49
- data/lib/mocha/central.rb +21 -12
- data/lib/mocha/change_state_side_effect.rb +0 -4
- data/lib/mocha/class_methods.rb +19 -20
- data/lib/mocha/configuration.rb +317 -52
- data/lib/mocha/debug.rb +2 -4
- data/lib/mocha/deprecation.rb +8 -11
- data/lib/mocha/detection/mini_test.rb +0 -2
- data/lib/mocha/detection/test_unit.rb +3 -5
- data/lib/mocha/error_with_filtered_backtrace.rb +13 -0
- data/lib/mocha/exception_raiser.rb +4 -6
- data/lib/mocha/expectation.rb +186 -95
- data/lib/mocha/expectation_error.rb +1 -1
- data/lib/mocha/expectation_error_factory.rb +0 -1
- data/lib/mocha/expectation_list.rb +7 -11
- data/lib/mocha/hooks.rb +1 -3
- data/lib/mocha/in_state_ordering_constraint.rb +0 -4
- data/lib/mocha/inspect.rb +30 -38
- data/lib/mocha/instance_method.rb +11 -8
- data/lib/mocha/integration/mini_test/adapter.rb +3 -5
- data/lib/mocha/integration/mini_test/exception_translation.rb +1 -1
- data/lib/mocha/integration/mini_test.rb +10 -31
- data/lib/mocha/integration/monkey_patcher.rb +8 -2
- data/lib/mocha/integration/test_unit/adapter.rb +8 -9
- data/lib/mocha/integration/test_unit.rb +10 -26
- data/lib/mocha/invocation.rb +73 -0
- data/lib/mocha/is_a.rb +0 -2
- data/lib/mocha/logger.rb +0 -4
- data/lib/mocha/macos_version.rb +5 -0
- data/lib/mocha/method_matcher.rb +1 -5
- data/lib/mocha/minitest.rb +6 -0
- data/lib/mocha/mock.rb +105 -57
- data/lib/mocha/mockery.rb +70 -98
- data/lib/mocha/names.rb +2 -12
- data/lib/mocha/not_initialized_error.rb +7 -0
- data/lib/mocha/object_methods.rb +25 -31
- data/lib/mocha/parameter_matchers/all_of.rb +2 -8
- data/lib/mocha/parameter_matchers/any_of.rb +2 -8
- data/lib/mocha/parameter_matchers/any_parameters.rb +3 -9
- data/lib/mocha/parameter_matchers/anything.rb +2 -8
- data/lib/mocha/parameter_matchers/base.rb +7 -13
- data/lib/mocha/parameter_matchers/equals.rb +1 -7
- data/lib/mocha/parameter_matchers/{query_string.rb → equivalent_uri.rb} +14 -15
- data/lib/mocha/parameter_matchers/has_entries.rb +2 -7
- data/lib/mocha/parameter_matchers/has_entry.rb +26 -21
- data/lib/mocha/parameter_matchers/has_key.rb +2 -7
- data/lib/mocha/parameter_matchers/has_keys.rb +53 -0
- data/lib/mocha/parameter_matchers/has_value.rb +2 -7
- data/lib/mocha/parameter_matchers/includes.rb +50 -8
- data/lib/mocha/parameter_matchers/instance_methods.rb +27 -0
- data/lib/mocha/parameter_matchers/instance_of.rb +0 -6
- data/lib/mocha/parameter_matchers/is_a.rb +2 -7
- data/lib/mocha/parameter_matchers/kind_of.rb +2 -6
- data/lib/mocha/parameter_matchers/not.rb +2 -7
- data/lib/mocha/parameter_matchers/optionally.rb +4 -10
- data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +64 -0
- data/lib/mocha/parameter_matchers/regexp_matches.rb +0 -6
- data/lib/mocha/parameter_matchers/responds_with.rb +3 -8
- data/lib/mocha/parameter_matchers/yaml_equivalent.rb +2 -6
- data/lib/mocha/parameter_matchers.rb +3 -4
- data/lib/mocha/parameters_matcher.rb +8 -11
- data/lib/mocha/raised_exception.rb +11 -0
- data/lib/mocha/receivers.rb +10 -14
- data/lib/mocha/return_values.rb +4 -8
- data/lib/mocha/ruby_version.rb +3 -0
- data/lib/mocha/sequence.rb +4 -9
- data/lib/mocha/single_return_value.rb +2 -5
- data/lib/mocha/state_machine.rb +33 -46
- data/lib/mocha/stubbed_method.rb +88 -0
- data/lib/mocha/stubbing_error.rb +2 -13
- data/lib/mocha/test_unit.rb +5 -2
- data/lib/mocha/thrower.rb +4 -6
- data/lib/mocha/thrown_object.rb +12 -0
- data/lib/mocha/version.rb +1 -1
- data/lib/mocha/yield_parameters.rb +7 -17
- data/mocha.gemspec +16 -43
- metadata +38 -248
- data/bin/build-matrix +0 -71
- 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.11.0 +0 -7
- data/gemfiles/Gemfile.minitest.2.11.2 +0 -7
- data/gemfiles/Gemfile.minitest.2.3.0 +0 -7
- data/gemfiles/Gemfile.test-unit.2.0.0 +0 -7
- data/gemfiles/Gemfile.test-unit.2.0.1 +0 -7
- data/gemfiles/Gemfile.test-unit.2.0.3 +0 -7
- data/init.rb +0 -3
- data/lib/mocha/class_method.rb +0 -127
- data/lib/mocha/integration/mini_test/nothing.rb +0 -19
- data/lib/mocha/integration/mini_test/version_13.rb +0 -51
- data/lib/mocha/integration/mini_test/version_140.rb +0 -51
- data/lib/mocha/integration/mini_test/version_141.rb +0 -62
- data/lib/mocha/integration/mini_test/version_142_to_172.rb +0 -62
- data/lib/mocha/integration/mini_test/version_200.rb +0 -63
- data/lib/mocha/integration/mini_test/version_201_to_222.rb +0 -63
- data/lib/mocha/integration/mini_test/version_2110_to_2111.rb +0 -67
- data/lib/mocha/integration/mini_test/version_2112_to_320.rb +0 -70
- data/lib/mocha/integration/mini_test/version_230_to_2101.rb +0 -65
- data/lib/mocha/integration/test_unit/gem_version_200.rb +0 -59
- data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +0 -59
- data/lib/mocha/integration/test_unit/gem_version_203_to_220.rb +0 -59
- data/lib/mocha/integration/test_unit/gem_version_230_to_250.rb +0 -65
- data/lib/mocha/integration/test_unit/nothing.rb +0 -19
- data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +0 -58
- data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +0 -60
- data/lib/mocha/integration.rb +0 -14
- data/lib/mocha/mini_test.rb +0 -3
- data/lib/mocha/module_method.rb +0 -16
- data/lib/mocha/module_methods.rb +0 -14
- data/lib/mocha/multiple_yields.rb +0 -20
- data/lib/mocha/no_yields.rb +0 -11
- data/lib/mocha/parameter_matchers/object.rb +0 -17
- data/lib/mocha/pretty_parameters.rb +0 -28
- data/lib/mocha/setup.rb +0 -9
- data/lib/mocha/single_yield.rb +0 -18
- data/lib/mocha/standalone.rb +0 -4
- data/lib/mocha/unexpected_invocation.rb +0 -26
- data/lib/mocha_standalone.rb +0 -4
- data/test/acceptance/acceptance_test_helper.rb +0 -41
- 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/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 -337
- data/test/acceptance/partial_mocks_test.rb +0 -47
- data/test/acceptance/prepend_test.rb +0 -88
- 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_defined_on_superclass_test.rb +0 -34
- data/test/acceptance/stub_any_instance_method_test.rb +0 -238
- 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 -78
- 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 -112
- 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 -69
- 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_result_test.rb +0 -66
- data/test/acceptance/stubbing_error_backtrace_test.rb +0 -64
- data/test/acceptance/stubbing_frozen_object_test.rb +0 -88
- data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +0 -48
- data/test/acceptance/stubbing_method_unnecessarily_test.rb +0 -65
- data/test/acceptance/stubbing_nil_test.rb +0 -59
- 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/stubbing_same_class_method_on_parent_and_child_classes_test.rb +0 -35
- data/test/acceptance/throw_test.rb +0 -45
- data/test/acceptance/unexpected_invocation_test.rb +0 -25
- data/test/acceptance/unstubbing_test.rb +0 -168
- data/test/assertions.rb +0 -6
- data/test/deprecation_disabler.rb +0 -15
- data/test/execution_point.rb +0 -36
- data/test/integration/mini_test_test.rb +0 -8
- data/test/integration/shared_tests.rb +0 -174
- data/test/integration/test_unit_test.rb +0 -8
- data/test/method_definer.rb +0 -24
- data/test/mini_test_result.rb +0 -90
- data/test/minitest_result.rb +0 -49
- data/test/simple_counter.rb +0 -13
- data/test/test_helper.rb +0 -50
- data/test/test_runner.rb +0 -58
- data/test/test_unit_result.rb +0 -20
- data/test/unit/any_instance_method_test.rb +0 -132
- 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 -223
- data/test/unit/class_methods_test.rb +0 -40
- 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 -82
- data/test/unit/expectation_test.rb +0 -497
- data/test/unit/hash_inspect_test.rb +0 -16
- data/test/unit/hooks_test.rb +0 -29
- data/test/unit/in_state_ordering_constraint_test.rb +0 -43
- data/test/unit/method_matcher_test.rb +0 -28
- data/test/unit/mock_test.rb +0 -341
- data/test/unit/mockery_test.rb +0 -151
- data/test/unit/module_methods_test.rb +0 -19
- 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_methods_test.rb +0 -46
- 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 -129
- 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 -59
- 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 -32
- 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/receivers_test.rb +0 -66
- 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/yard-templates/default/layout/html/google_analytics.erb +0 -11
- data/yard-templates/default/layout/html/setup.rb +0 -6
data/test/test_runner.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
require 'assertions'
|
2
|
-
|
3
|
-
require 'mocha/detection/mini_test'
|
4
|
-
|
5
|
-
module TestRunner
|
6
|
-
def run_as_test(&block)
|
7
|
-
run_as_tests(:test_me => block)
|
8
|
-
end
|
9
|
-
|
10
|
-
def run_as_tests(methods = {})
|
11
|
-
base_class = Mocha::TestCase
|
12
|
-
test_class = Class.new(base_class) do
|
13
|
-
include Assertions
|
14
|
-
|
15
|
-
methods.each do |(method_name, proc)|
|
16
|
-
define_method(method_name, proc)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
tests = methods.keys.select { |m| m.to_s[/^test/] }.map { |m| test_class.new(m) }
|
21
|
-
|
22
|
-
if Mocha::Detection::MiniTest.testcase && (ENV['MOCHA_RUN_INTEGRATION_TESTS'] != 'test-unit')
|
23
|
-
minitest_version = Gem::Version.new(Mocha::Detection::MiniTest.version)
|
24
|
-
if Gem::Requirement.new('>= 5.0.0').satisfied_by?(minitest_version)
|
25
|
-
require File.expand_path('../minitest_result', __FILE__)
|
26
|
-
tests.each do |test|
|
27
|
-
test.run
|
28
|
-
end
|
29
|
-
Minitest::Runnable.runnables.delete(test_class)
|
30
|
-
test_result = MinitestResult.new(tests)
|
31
|
-
elsif Gem::Requirement.new('> 0.0.0', '< 5.0.0').satisfied_by?(minitest_version)
|
32
|
-
require File.expand_path('../mini_test_result', __FILE__)
|
33
|
-
runner = MiniTest::Unit.new
|
34
|
-
tests.each do |test|
|
35
|
-
test.run(runner)
|
36
|
-
end
|
37
|
-
test_result = MiniTestResult.new(runner, tests)
|
38
|
-
end
|
39
|
-
else
|
40
|
-
require File.expand_path('../test_unit_result', __FILE__)
|
41
|
-
test_result = TestUnitResult.build_test_result
|
42
|
-
tests.each do |test|
|
43
|
-
test.run(test_result) {}
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
test_result
|
48
|
-
end
|
49
|
-
|
50
|
-
def assert_passed(test_result)
|
51
|
-
flunk "Test failed unexpectedly with message: #{test_result.failures}" if test_result.failure_count > 0
|
52
|
-
flunk "Test failed unexpectedly with message: #{test_result.errors}" if test_result.error_count > 0
|
53
|
-
end
|
54
|
-
|
55
|
-
def assert_failed(test_result)
|
56
|
-
flunk "Test passed unexpectedly" unless test_result.failure_count + test_result.error_count > 0
|
57
|
-
end
|
58
|
-
end
|
data/test/test_unit_result.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'test/unit/testresult'
|
2
|
-
|
3
|
-
class TestUnitResult
|
4
|
-
def self.build_test_result
|
5
|
-
test_result = Test::Unit::TestResult.new
|
6
|
-
class << test_result
|
7
|
-
attr_reader :failures, :errors
|
8
|
-
def failure_messages
|
9
|
-
failures.map { |failure| failure.message }
|
10
|
-
end
|
11
|
-
def failure_message_lines
|
12
|
-
failure_messages.map { |message| message.split("\n") }.flatten
|
13
|
-
end
|
14
|
-
def error_messages
|
15
|
-
errors.map { |error| error.message }
|
16
|
-
end
|
17
|
-
end
|
18
|
-
test_result
|
19
|
-
end
|
20
|
-
end
|
@@ -1,132 +0,0 @@
|
|
1
|
-
require File.expand_path('../../test_helper', __FILE__)
|
2
|
-
require 'method_definer'
|
3
|
-
require 'mocha/mock'
|
4
|
-
require 'mocha/any_instance_method'
|
5
|
-
|
6
|
-
class AnyInstanceMethodTest < Mocha::TestCase
|
7
|
-
|
8
|
-
include Mocha
|
9
|
-
|
10
|
-
def test_should_hide_original_method
|
11
|
-
klass = Class.new { def method_x; end }
|
12
|
-
method = AnyInstanceMethod.new(klass, :method_x)
|
13
|
-
|
14
|
-
method.hide_original_method
|
15
|
-
|
16
|
-
assert_equal false, klass.method_defined?(:method_x)
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_should_not_raise_error_hiding_method_that_isnt_defined
|
20
|
-
klass = Class.new
|
21
|
-
method = AnyInstanceMethod.new(klass, :method_x)
|
22
|
-
|
23
|
-
assert_nothing_raised { method.hide_original_method }
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_should_define_a_new_method
|
27
|
-
klass = Class.new { def method_x; end }
|
28
|
-
method = AnyInstanceMethod.new(klass, :method_x)
|
29
|
-
mocha = build_mock
|
30
|
-
mocha.expects(:method_x).with(:param1, :param2).returns(:result)
|
31
|
-
any_instance = Object.new
|
32
|
-
any_instance.define_instance_method(:mocha) { mocha }
|
33
|
-
klass.define_instance_method(:any_instance) { any_instance }
|
34
|
-
|
35
|
-
method.hide_original_method
|
36
|
-
method.define_new_method
|
37
|
-
|
38
|
-
instance = klass.new
|
39
|
-
result = instance.method_x(:param1, :param2)
|
40
|
-
|
41
|
-
assert_equal :result, result
|
42
|
-
assert mocha.__verified__?
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_should_restore_original_method
|
46
|
-
klass = Class.new { def method_x; :original_result; end }
|
47
|
-
method = AnyInstanceMethod.new(klass, :method_x)
|
48
|
-
|
49
|
-
method.hide_original_method
|
50
|
-
method.define_new_method
|
51
|
-
method.remove_new_method
|
52
|
-
method.restore_original_method
|
53
|
-
|
54
|
-
instance = klass.new
|
55
|
-
assert instance.respond_to?(:method_x)
|
56
|
-
assert_equal :original_result, instance.method_x
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_should_not_restore_original_method_if_none_was_defined_in_first_place
|
60
|
-
klass = Class.new { def method_x; :new_result; end }
|
61
|
-
method = AnyInstanceMethod.new(klass, :method_x)
|
62
|
-
|
63
|
-
method.restore_original_method
|
64
|
-
|
65
|
-
instance = klass.new
|
66
|
-
assert_equal :new_result, instance.method_x
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_should_call_remove_new_method
|
70
|
-
klass = Class.new { def method_x; end }
|
71
|
-
any_instance = build_mock
|
72
|
-
any_instance_mocha = build_mock
|
73
|
-
any_instance.stubs(:mocha).returns(any_instance_mocha)
|
74
|
-
klass.define_instance_method(:any_instance) { any_instance }
|
75
|
-
method = AnyInstanceMethod.new(klass, :method_x)
|
76
|
-
method.replace_instance_method(:restore_original_method) { }
|
77
|
-
method.define_instance_accessor(:remove_called)
|
78
|
-
method.replace_instance_method(:remove_new_method) { self.remove_called = true }
|
79
|
-
|
80
|
-
method.unstub
|
81
|
-
|
82
|
-
assert method.remove_called
|
83
|
-
end
|
84
|
-
|
85
|
-
def test_should_call_restore_original_method
|
86
|
-
klass = Class.new { def method_x; end }
|
87
|
-
any_instance = build_mock
|
88
|
-
any_instance_mocha = build_mock
|
89
|
-
any_instance.stubs(:mocha).returns(any_instance_mocha)
|
90
|
-
klass.define_instance_method(:any_instance) { any_instance }
|
91
|
-
method = AnyInstanceMethod.new(klass, :method_x)
|
92
|
-
method.replace_instance_method(:remove_new_method) { }
|
93
|
-
method.define_instance_accessor(:restore_called)
|
94
|
-
method.replace_instance_method(:restore_original_method) { self.restore_called = true }
|
95
|
-
|
96
|
-
method.unstub
|
97
|
-
|
98
|
-
assert method.restore_called
|
99
|
-
end
|
100
|
-
|
101
|
-
def test_should_call_mock_unstub
|
102
|
-
klass = Class.new { def method_x; end }
|
103
|
-
|
104
|
-
method = AnyInstanceMethod.new(klass, :method_x)
|
105
|
-
|
106
|
-
method.replace_instance_method(:remove_new_method) { }
|
107
|
-
method.replace_instance_method(:restore_original_method) { }
|
108
|
-
mocha = Class.new { class << self; attr_accessor :unstub_method; end; def self.unstub(method); self.unstub_method = method; end; }
|
109
|
-
mocha.define_instance_method(:any_expectations?) { true }
|
110
|
-
method.replace_instance_method(:mock) { mocha }
|
111
|
-
|
112
|
-
method.unstub
|
113
|
-
|
114
|
-
assert_equal mocha.unstub_method, :method_x
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_should_return_any_instance_mocha_for_stubbee
|
118
|
-
mocha = Object.new
|
119
|
-
any_instance = Object.new
|
120
|
-
any_instance.define_instance_method(:mocha) { mocha }
|
121
|
-
stubbee = Class.new
|
122
|
-
stubbee.define_instance_method(:any_instance) { any_instance }
|
123
|
-
method = AnyInstanceMethod.new(stubbee, :method_name)
|
124
|
-
assert_equal stubbee.any_instance.mocha, method.mock
|
125
|
-
end
|
126
|
-
|
127
|
-
private
|
128
|
-
|
129
|
-
def build_mock
|
130
|
-
Mock.new(nil)
|
131
|
-
end
|
132
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
require File.expand_path('../../test_helper', __FILE__)
|
2
|
-
require 'mocha/inspect'
|
3
|
-
|
4
|
-
class ArrayInspectTest < Mocha::TestCase
|
5
|
-
|
6
|
-
def test_should_use_inspect
|
7
|
-
array = [1, 2]
|
8
|
-
assert_equal array.inspect, array.mocha_inspect
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_should_use_mocha_inspect_on_each_item
|
12
|
-
array = [1, 2, "chris"]
|
13
|
-
assert_equal "[1, 2, 'chris']", array.mocha_inspect
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require File.expand_path('../../test_helper', __FILE__)
|
2
|
-
require 'mocha/backtrace_filter'
|
3
|
-
|
4
|
-
class BacktraceFilterTest < Mocha::TestCase
|
5
|
-
|
6
|
-
include Mocha
|
7
|
-
|
8
|
-
def test_should_exclude_mocha_locations_from_backtrace
|
9
|
-
mocha_lib = "/username/workspace/mocha_wibble/lib/"
|
10
|
-
backtrace = [ mocha_lib + 'exclude/me/1', mocha_lib + 'exclude/me/2', '/keep/me', mocha_lib + 'exclude/me/3']
|
11
|
-
filter = BacktraceFilter.new(mocha_lib)
|
12
|
-
assert_equal ['/keep/me'], filter.filtered(backtrace)
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_should_determine_path_for_mocha_lib_directory
|
16
|
-
assert_match Regexp.new("/lib/$"), BacktraceFilter::LIB_DIRECTORY
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
@@ -1,56 +0,0 @@
|
|
1
|
-
require File.expand_path('../../test_helper', __FILE__)
|
2
|
-
require 'mocha/cardinality'
|
3
|
-
|
4
|
-
class CardinalityTest < Mocha::TestCase
|
5
|
-
|
6
|
-
include Mocha
|
7
|
-
|
8
|
-
def test_should_allow_invocations_if_invocation_count_has_not_yet_reached_maximum
|
9
|
-
cardinality = Cardinality.new(2, 3)
|
10
|
-
assert cardinality.invocations_allowed?(0)
|
11
|
-
assert cardinality.invocations_allowed?(1)
|
12
|
-
assert cardinality.invocations_allowed?(2)
|
13
|
-
assert !cardinality.invocations_allowed?(3)
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_should_be_satisfied_if_invocations_so_far_have_reached_required_threshold
|
17
|
-
cardinality = Cardinality.new(2, 3)
|
18
|
-
assert !cardinality.satisfied?(0)
|
19
|
-
assert !cardinality.satisfied?(1)
|
20
|
-
assert cardinality.satisfied?(2)
|
21
|
-
assert cardinality.satisfied?(3)
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_should_describe_cardinality
|
25
|
-
assert_equal 'allowed any number of times', Cardinality.at_least(0).mocha_inspect
|
26
|
-
|
27
|
-
assert_equal 'expected at most once', Cardinality.at_most(1).mocha_inspect
|
28
|
-
assert_equal 'expected at most twice', Cardinality.at_most(2).mocha_inspect
|
29
|
-
assert_equal 'expected at most 3 times', Cardinality.at_most(3).mocha_inspect
|
30
|
-
|
31
|
-
assert_equal 'expected at least once', Cardinality.at_least(1).mocha_inspect
|
32
|
-
assert_equal 'expected at least twice', Cardinality.at_least(2).mocha_inspect
|
33
|
-
assert_equal 'expected at least 3 times', Cardinality.at_least(3).mocha_inspect
|
34
|
-
|
35
|
-
assert_equal 'expected never', Cardinality.exactly(0).mocha_inspect
|
36
|
-
assert_equal 'expected exactly once', Cardinality.exactly(1).mocha_inspect
|
37
|
-
assert_equal 'expected exactly twice', Cardinality.exactly(2).mocha_inspect
|
38
|
-
assert_equal 'expected exactly 3 times', Cardinality.times(3).mocha_inspect
|
39
|
-
|
40
|
-
assert_equal 'expected between 2 and 4 times', Cardinality.times(2..4).mocha_inspect
|
41
|
-
assert_equal 'expected between 1 and 3 times', Cardinality.times(1..3).mocha_inspect
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_should_need_verifying
|
45
|
-
assert Cardinality.exactly(2).needs_verifying?
|
46
|
-
assert Cardinality.at_least(3).needs_verifying?
|
47
|
-
assert Cardinality.at_most(2).needs_verifying?
|
48
|
-
assert Cardinality.times(4).needs_verifying?
|
49
|
-
assert Cardinality.times(2..4).needs_verifying?
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_should_not_need_verifying
|
53
|
-
assert_equal false, Cardinality.at_least(0).needs_verifying?
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
data/test/unit/central_test.rb
DELETED
@@ -1,100 +0,0 @@
|
|
1
|
-
require File.expand_path('../../test_helper', __FILE__)
|
2
|
-
|
3
|
-
require 'mocha/central'
|
4
|
-
require 'mocha/mock'
|
5
|
-
require 'method_definer'
|
6
|
-
|
7
|
-
class CentralTest < Mocha::TestCase
|
8
|
-
|
9
|
-
include Mocha
|
10
|
-
|
11
|
-
def test_should_start_with_empty_stubba_methods
|
12
|
-
stubba = Central.new
|
13
|
-
|
14
|
-
assert_equal [], stubba.stubba_methods
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_should_stub_method_if_not_already_stubbed
|
18
|
-
method = build_mock
|
19
|
-
method.expects(:stub)
|
20
|
-
stubba = Central.new
|
21
|
-
|
22
|
-
stubba.stub(method)
|
23
|
-
|
24
|
-
assert method.__verified__?
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_should_not_stub_method_if_already_stubbed
|
28
|
-
method = build_mock
|
29
|
-
method.stubs(:matches?).returns(true)
|
30
|
-
method.expects(:stub).times(0)
|
31
|
-
stubba = Central.new
|
32
|
-
stubba.stubba_methods = [method]
|
33
|
-
|
34
|
-
stubba.stub(method)
|
35
|
-
|
36
|
-
assert method.__verified__?
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_should_record_method
|
40
|
-
method = build_mock
|
41
|
-
method.expects(:stub)
|
42
|
-
stubba = Central.new
|
43
|
-
|
44
|
-
stubba.stub(method)
|
45
|
-
|
46
|
-
assert_equal [method], stubba.stubba_methods
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_should_unstub_specified_method
|
50
|
-
stubba = Central.new
|
51
|
-
method_1 = build_mock
|
52
|
-
method_1.stubs(:matches?).returns(false)
|
53
|
-
method_2 = build_mock
|
54
|
-
method_2.stubs(:matches?).returns(true)
|
55
|
-
method_2.expects(:unstub)
|
56
|
-
stubba.stubba_methods = [method_1, method_2]
|
57
|
-
|
58
|
-
stubba.unstub(method_2)
|
59
|
-
|
60
|
-
assert_equal [method_1], stubba.stubba_methods
|
61
|
-
assert method_2.__verified__?
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_should_not_unstub_specified_method_if_not_already_stubbed
|
65
|
-
stubba = Central.new
|
66
|
-
method_1 = build_mock
|
67
|
-
method_1.stubs(:matches?).returns(false)
|
68
|
-
method_2 = build_mock
|
69
|
-
method_2.expects(:unstub).never
|
70
|
-
stubba.stubba_methods = [method_1]
|
71
|
-
|
72
|
-
stubba.unstub(method_2)
|
73
|
-
|
74
|
-
assert_equal [method_1], stubba.stubba_methods
|
75
|
-
assert method_2.__verified__?
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_should_unstub_all_methods
|
79
|
-
stubba = Central.new
|
80
|
-
method_1 = build_mock
|
81
|
-
method_1.stubs(:matches?).returns(true)
|
82
|
-
method_1.expects(:unstub)
|
83
|
-
method_2 = build_mock
|
84
|
-
method_2.stubs(:matches?).returns(true)
|
85
|
-
method_2.expects(:unstub)
|
86
|
-
stubba.stubba_methods = [method_1, method_2]
|
87
|
-
|
88
|
-
stubba.unstub_all
|
89
|
-
|
90
|
-
assert_equal [], stubba.stubba_methods
|
91
|
-
assert method_1.__verified__?
|
92
|
-
assert method_2.__verified__?
|
93
|
-
end
|
94
|
-
|
95
|
-
private
|
96
|
-
|
97
|
-
def build_mock
|
98
|
-
Mock.new(nil)
|
99
|
-
end
|
100
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
require File.expand_path('../../test_helper', __FILE__)
|
2
|
-
|
3
|
-
require 'mocha/change_state_side_effect'
|
4
|
-
|
5
|
-
class ChangeStateSideEffectTest < Mocha::TestCase
|
6
|
-
|
7
|
-
include Mocha
|
8
|
-
|
9
|
-
class FakeState
|
10
|
-
|
11
|
-
attr_reader :active
|
12
|
-
attr_writer :description
|
13
|
-
|
14
|
-
def activate
|
15
|
-
@active = true
|
16
|
-
end
|
17
|
-
|
18
|
-
def mocha_inspect
|
19
|
-
@description
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_should_activate_the_given_state
|
25
|
-
state = FakeState.new
|
26
|
-
side_effect = ChangeStateSideEffect.new(state)
|
27
|
-
|
28
|
-
side_effect.perform
|
29
|
-
|
30
|
-
assert state.active
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_should_describe_itself_in_terms_of_the_activated_state
|
34
|
-
state = FakeState.new
|
35
|
-
state.description = 'the-new-state'
|
36
|
-
side_effect = ChangeStateSideEffect.new(state)
|
37
|
-
|
38
|
-
assert_equal 'then the-new-state', side_effect.mocha_inspect
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
@@ -1,223 +0,0 @@
|
|
1
|
-
require File.expand_path('../../test_helper', __FILE__)
|
2
|
-
require 'method_definer'
|
3
|
-
require 'mocha/mock'
|
4
|
-
|
5
|
-
require 'mocha/class_method'
|
6
|
-
|
7
|
-
class ClassMethodTest < Mocha::TestCase
|
8
|
-
|
9
|
-
include Mocha
|
10
|
-
|
11
|
-
def test_should_hide_original_method
|
12
|
-
klass = Class.new { def self.method_x; end }
|
13
|
-
method = ClassMethod.new(klass, :method_x)
|
14
|
-
|
15
|
-
method.hide_original_method
|
16
|
-
|
17
|
-
assert_equal false, klass.respond_to?(:method_x)
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_should_not_raise_error_hiding_method_that_isnt_defined
|
21
|
-
klass = Class.new
|
22
|
-
method = ClassMethod.new(klass, :method_x)
|
23
|
-
|
24
|
-
assert_nothing_raised { method.hide_original_method }
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_should_not_raise_error_hiding_method_in_class_that_implements_method_called_method
|
28
|
-
klass = Class.new { def self.method; end }
|
29
|
-
method = ClassMethod.new(klass, :method)
|
30
|
-
|
31
|
-
assert_nothing_raised { method.hide_original_method }
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_should_define_a_new_method_which_should_call_mocha_method_missing
|
35
|
-
klass = Class.new { def self.method_x; end }
|
36
|
-
mocha = build_mock
|
37
|
-
klass.define_instance_method(:mocha) { mocha }
|
38
|
-
mocha.expects(:method_x).with(:param1, :param2).returns(:result)
|
39
|
-
method = ClassMethod.new(klass, :method_x)
|
40
|
-
|
41
|
-
method.hide_original_method
|
42
|
-
method.define_new_method
|
43
|
-
result = klass.method_x(:param1, :param2)
|
44
|
-
|
45
|
-
assert_equal :result, result
|
46
|
-
assert mocha.__verified__?
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_should_remove_new_method
|
50
|
-
klass = Class.new { def self.method_x; end }
|
51
|
-
method = ClassMethod.new(klass, :method_x)
|
52
|
-
|
53
|
-
method.remove_new_method
|
54
|
-
|
55
|
-
assert_equal false, klass.respond_to?(:method_x)
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_should_restore_original_method
|
59
|
-
klass = Class.new { def self.method_x; :original_result; end }
|
60
|
-
method = ClassMethod.new(klass, :method_x)
|
61
|
-
|
62
|
-
method.hide_original_method
|
63
|
-
method.define_new_method
|
64
|
-
method.remove_new_method
|
65
|
-
method.restore_original_method
|
66
|
-
|
67
|
-
assert klass.respond_to?(:method_x)
|
68
|
-
assert_equal :original_result, klass.method_x
|
69
|
-
end
|
70
|
-
|
71
|
-
def test_should_restore_original_method_accepting_a_block_parameter
|
72
|
-
klass = Class.new { def self.method_x(&block); block.call if block_given? ; end }
|
73
|
-
method = ClassMethod.new(klass, :method_x)
|
74
|
-
|
75
|
-
method.hide_original_method
|
76
|
-
method.define_new_method
|
77
|
-
method.remove_new_method
|
78
|
-
method.restore_original_method
|
79
|
-
|
80
|
-
block_called = false
|
81
|
-
klass.method_x { block_called = true }
|
82
|
-
assert block_called
|
83
|
-
end
|
84
|
-
|
85
|
-
def test_should_not_restore_original_method_if_none_was_defined_in_first_place
|
86
|
-
klass = Class.new { def self.method_x; :new_result; end }
|
87
|
-
method = ClassMethod.new(klass, :method_x)
|
88
|
-
|
89
|
-
method.restore_original_method
|
90
|
-
|
91
|
-
assert_equal :new_result, klass.method_x
|
92
|
-
end
|
93
|
-
|
94
|
-
def test_should_call_hide_original_method
|
95
|
-
klass = Class.new { def self.method_x; end }
|
96
|
-
method = ClassMethod.new(klass, :method_x)
|
97
|
-
method.hide_original_method
|
98
|
-
method.define_instance_accessor(:hide_called)
|
99
|
-
method.replace_instance_method(:hide_original_method) { self.hide_called = true }
|
100
|
-
|
101
|
-
method.stub
|
102
|
-
|
103
|
-
assert method.hide_called
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_should_call_define_new_method
|
107
|
-
klass = Class.new { def self.method_x; end }
|
108
|
-
method = ClassMethod.new(klass, :method_x)
|
109
|
-
method.define_instance_accessor(:define_called)
|
110
|
-
method.replace_instance_method(:define_new_method) { self.define_called = true }
|
111
|
-
|
112
|
-
method.stub
|
113
|
-
|
114
|
-
assert method.define_called
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_should_call_remove_new_method
|
118
|
-
klass = Class.new { def self.method_x; end }
|
119
|
-
method = ClassMethod.new(klass, :method_x)
|
120
|
-
mocha = build_mock
|
121
|
-
klass.define_instance_method(:mocha) { mocha }
|
122
|
-
method.define_instance_accessor(:remove_called)
|
123
|
-
method.replace_instance_method(:remove_new_method) { self.remove_called = true }
|
124
|
-
|
125
|
-
method.unstub
|
126
|
-
|
127
|
-
assert method.remove_called
|
128
|
-
end
|
129
|
-
|
130
|
-
def test_should_call_restore_original_method
|
131
|
-
klass = Class.new { def self.method_x; end }
|
132
|
-
mocha = build_mock
|
133
|
-
klass.define_instance_method(:mocha) { mocha }
|
134
|
-
method = ClassMethod.new(klass, :method_x)
|
135
|
-
method.define_instance_accessor(:restore_called)
|
136
|
-
method.replace_instance_method(:restore_original_method) { self.restore_called = true }
|
137
|
-
|
138
|
-
method.unstub
|
139
|
-
|
140
|
-
assert method.restore_called
|
141
|
-
end
|
142
|
-
|
143
|
-
def test_should_call_mocha_unstub
|
144
|
-
klass = Class.new { def self.method_x; end }
|
145
|
-
method = ClassMethod.new(klass, :method_x)
|
146
|
-
method.replace_instance_method(:restore_original_method) { }
|
147
|
-
mocha = Class.new { class << self; attr_accessor :unstub_method; end; def self.unstub(method); self.unstub_method = method; end; }
|
148
|
-
mocha.define_instance_method(:any_expectations?) { true }
|
149
|
-
method.replace_instance_method(:mock) { mocha }
|
150
|
-
|
151
|
-
method.unstub
|
152
|
-
assert_equal mocha.unstub_method, :method_x
|
153
|
-
end
|
154
|
-
|
155
|
-
def test_should_call_stubbee_reset_mocha_if_no_expectations_remaining
|
156
|
-
klass = Class.new { def self.method_x; end }
|
157
|
-
method = ClassMethod.new(klass, :method_x)
|
158
|
-
method.replace_instance_method(:remove_new_method) { }
|
159
|
-
method.replace_instance_method(:restore_original_method) { }
|
160
|
-
mocha = Class.new
|
161
|
-
mocha.define_instance_method(:unstub) { |method_name| }
|
162
|
-
mocha.define_instance_method(:any_expectations?) { false }
|
163
|
-
method.replace_instance_method(:mock) { mocha }
|
164
|
-
stubbee = Class.new { attr_accessor :reset_mocha_called; def reset_mocha; self.reset_mocha_called = true; end; }.new
|
165
|
-
method.replace_instance_method(:stubbee) { stubbee }
|
166
|
-
|
167
|
-
method.unstub
|
168
|
-
|
169
|
-
assert stubbee.reset_mocha_called
|
170
|
-
end
|
171
|
-
|
172
|
-
def test_should_return_mock_for_stubbee
|
173
|
-
mocha = Object.new
|
174
|
-
stubbee = Object.new
|
175
|
-
stubbee.define_instance_accessor(:mocha) { mocha }
|
176
|
-
stubbee.mocha = nil
|
177
|
-
method = ClassMethod.new(stubbee, :method_name)
|
178
|
-
assert_equal stubbee.mocha, method.mock
|
179
|
-
end
|
180
|
-
|
181
|
-
def test_should_not_match_if_other_object_has_a_different_class
|
182
|
-
class_method = ClassMethod.new(Object.new, :method)
|
183
|
-
other_object = Object.new
|
184
|
-
assert !class_method.matches?(other_object)
|
185
|
-
end
|
186
|
-
|
187
|
-
def test_should_not_match_if_other_class_method_has_different_stubbee
|
188
|
-
stubbee_1 = Object.new
|
189
|
-
stubbee_2 = Object.new
|
190
|
-
class_method_1 = ClassMethod.new(stubbee_1, :method)
|
191
|
-
class_method_2 = ClassMethod.new(stubbee_2, :method)
|
192
|
-
assert !class_method_1.matches?(class_method_2)
|
193
|
-
end
|
194
|
-
|
195
|
-
def test_should_not_match_if_other_class_method_has_different_method
|
196
|
-
stubbee = Object.new
|
197
|
-
class_method_1 = ClassMethod.new(stubbee, :method_1)
|
198
|
-
class_method_2 = ClassMethod.new(stubbee, :method_2)
|
199
|
-
assert !class_method_1.matches?(class_method_2)
|
200
|
-
end
|
201
|
-
|
202
|
-
def test_should_match_if_other_class_method_has_same_stubbee_and_same_method_so_no_attempt_is_made_to_stub_a_method_twice
|
203
|
-
stubbee = Object.new
|
204
|
-
class_method_1 = ClassMethod.new(stubbee, :method)
|
205
|
-
class_method_2 = ClassMethod.new(stubbee, :method)
|
206
|
-
assert class_method_1.matches?(class_method_2)
|
207
|
-
end
|
208
|
-
|
209
|
-
def test_should_match_if_other_class_method_has_same_stubbee_and_same_method_but_stubbee_equal_method_lies_like_active_record_association_proxy
|
210
|
-
stubbee = Class.new do
|
211
|
-
def equal?(other); false; end
|
212
|
-
end.new
|
213
|
-
class_method_1 = ClassMethod.new(stubbee, :method)
|
214
|
-
class_method_2 = ClassMethod.new(stubbee, :method)
|
215
|
-
assert class_method_1.matches?(class_method_2)
|
216
|
-
end
|
217
|
-
|
218
|
-
private
|
219
|
-
|
220
|
-
def build_mock
|
221
|
-
Mock.new(nil)
|
222
|
-
end
|
223
|
-
end
|