mocha-macruby 0.9.8.20100129120100
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 +3 -0
- data/MIT-LICENSE +7 -0
- data/README +39 -0
- data/RELEASE +294 -0
- data/Rakefile +214 -0
- data/examples/misc.rb +43 -0
- data/examples/mocha.rb +25 -0
- data/examples/stubba.rb +64 -0
- data/lib/mocha.rb +3 -0
- data/lib/mocha/any_instance_method.rb +59 -0
- data/lib/mocha/api.rb +173 -0
- data/lib/mocha/argument_iterator.rb +21 -0
- data/lib/mocha/backtrace_filter.rb +17 -0
- data/lib/mocha/cardinality.rb +95 -0
- data/lib/mocha/central.rb +27 -0
- data/lib/mocha/change_state_side_effect.rb +19 -0
- data/lib/mocha/class_method.rb +117 -0
- data/lib/mocha/configuration.rb +79 -0
- data/lib/mocha/deprecation.rb +22 -0
- data/lib/mocha/exception_raiser.rb +17 -0
- data/lib/mocha/expectation.rb +476 -0
- data/lib/mocha/expectation_error.rb +15 -0
- data/lib/mocha/expectation_list.rb +50 -0
- data/lib/mocha/in_state_ordering_constraint.rb +19 -0
- data/lib/mocha/inspect.rb +67 -0
- data/lib/mocha/instance_method.rb +16 -0
- data/lib/mocha/integration.rb +38 -0
- data/lib/mocha/integration/mini_test.rb +21 -0
- data/lib/mocha/integration/mini_test/assertion_counter.rb +23 -0
- data/lib/mocha/integration/mini_test/version_131_and_above.rb +50 -0
- data/lib/mocha/integration/test_unit.rb +40 -0
- data/lib/mocha/integration/test_unit/assertion_counter.rb +23 -0
- data/lib/mocha/integration/test_unit/gem_version_200.rb +49 -0
- data/lib/mocha/integration/test_unit/gem_version_201_and_above.rb +49 -0
- data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +48 -0
- data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +50 -0
- data/lib/mocha/is_a.rb +9 -0
- data/lib/mocha/logger.rb +15 -0
- data/lib/mocha/metaclass.rb +13 -0
- data/lib/mocha/method_matcher.rb +21 -0
- data/lib/mocha/mock.rb +200 -0
- data/lib/mocha/mockery.rb +181 -0
- data/lib/mocha/module_method.rb +16 -0
- data/lib/mocha/multiple_yields.rb +20 -0
- data/lib/mocha/names.rb +53 -0
- data/lib/mocha/no_yields.rb +11 -0
- data/lib/mocha/object.rb +187 -0
- data/lib/mocha/parameter_matchers.rb +27 -0
- data/lib/mocha/parameter_matchers/all_of.rb +42 -0
- data/lib/mocha/parameter_matchers/any_of.rb +47 -0
- data/lib/mocha/parameter_matchers/any_parameters.rb +40 -0
- data/lib/mocha/parameter_matchers/anything.rb +33 -0
- data/lib/mocha/parameter_matchers/base.rb +15 -0
- data/lib/mocha/parameter_matchers/equals.rb +42 -0
- data/lib/mocha/parameter_matchers/has_entries.rb +45 -0
- data/lib/mocha/parameter_matchers/has_entry.rb +57 -0
- data/lib/mocha/parameter_matchers/has_key.rb +43 -0
- data/lib/mocha/parameter_matchers/has_value.rb +43 -0
- data/lib/mocha/parameter_matchers/includes.rb +41 -0
- data/lib/mocha/parameter_matchers/instance_of.rb +42 -0
- data/lib/mocha/parameter_matchers/is_a.rb +42 -0
- data/lib/mocha/parameter_matchers/kind_of.rb +42 -0
- data/lib/mocha/parameter_matchers/not.rb +42 -0
- data/lib/mocha/parameter_matchers/object.rb +15 -0
- data/lib/mocha/parameter_matchers/optionally.rb +55 -0
- data/lib/mocha/parameter_matchers/regexp_matches.rb +44 -0
- data/lib/mocha/parameter_matchers/responds_with.rb +43 -0
- data/lib/mocha/parameter_matchers/yaml_equivalent.rb +43 -0
- data/lib/mocha/parameters_matcher.rb +37 -0
- data/lib/mocha/pretty_parameters.rb +28 -0
- data/lib/mocha/return_values.rb +31 -0
- data/lib/mocha/sequence.rb +42 -0
- data/lib/mocha/single_return_value.rb +17 -0
- data/lib/mocha/single_yield.rb +18 -0
- data/lib/mocha/standalone.rb +1 -0
- data/lib/mocha/state_machine.rb +91 -0
- data/lib/mocha/stubbing_error.rb +16 -0
- data/lib/mocha/unexpected_invocation.rb +18 -0
- data/lib/mocha/yield_parameters.rb +31 -0
- data/lib/mocha_standalone.rb +2 -0
- data/lib/stubba.rb +4 -0
- data/test/acceptance/acceptance_test_helper.rb +38 -0
- data/test/acceptance/api_test.rb +139 -0
- data/test/acceptance/bug_18914_test.rb +43 -0
- data/test/acceptance/bug_21465_test.rb +34 -0
- data/test/acceptance/bug_21563_test.rb +25 -0
- data/test/acceptance/expected_invocation_count_test.rb +196 -0
- data/test/acceptance/failure_messages_test.rb +64 -0
- data/test/acceptance/minitest_test.rb +153 -0
- data/test/acceptance/mocha_example_test.rb +98 -0
- data/test/acceptance/mocha_test_result_test.rb +84 -0
- data/test/acceptance/mock_test.rb +100 -0
- data/test/acceptance/mock_with_initializer_block_test.rb +51 -0
- data/test/acceptance/mocked_methods_dispatch_test.rb +78 -0
- data/test/acceptance/optional_parameters_test.rb +70 -0
- data/test/acceptance/parameter_matcher_test.rb +209 -0
- data/test/acceptance/partial_mocks_test.rb +47 -0
- data/test/acceptance/return_value_test.rb +52 -0
- data/test/acceptance/sequence_test.rb +186 -0
- data/test/acceptance/states_test.rb +70 -0
- data/test/acceptance/stub_any_instance_method_test.rb +195 -0
- data/test/acceptance/stub_class_method_test.rb +203 -0
- data/test/acceptance/stub_everything_test.rb +56 -0
- data/test/acceptance/stub_instance_method_test.rb +203 -0
- data/test/acceptance/stub_module_method_test.rb +163 -0
- data/test/acceptance/stub_test.rb +52 -0
- data/test/acceptance/stubba_example_test.rb +102 -0
- data/test/acceptance/stubba_test.rb +15 -0
- data/test/acceptance/stubba_test_result_test.rb +66 -0
- data/test/acceptance/stubbing_error_backtrace_test.rb +64 -0
- data/test/acceptance/stubbing_method_unnecessarily_test.rb +65 -0
- data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +130 -0
- data/test/acceptance/stubbing_non_existent_class_method_test.rb +157 -0
- data/test/acceptance/stubbing_non_existent_instance_method_test.rb +147 -0
- data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +130 -0
- data/test/acceptance/stubbing_non_public_class_method_test.rb +163 -0
- data/test/acceptance/stubbing_non_public_instance_method_test.rb +143 -0
- data/test/acceptance/stubbing_on_non_mock_object_test.rb +64 -0
- data/test/deprecation_disabler.rb +15 -0
- data/test/execution_point.rb +36 -0
- data/test/method_definer.rb +24 -0
- data/test/simple_counter.rb +13 -0
- data/test/test_helper.rb +25 -0
- data/test/test_runner.rb +33 -0
- data/test/unit/any_instance_method_test.rb +126 -0
- data/test/unit/array_inspect_test.rb +16 -0
- data/test/unit/backtrace_filter_test.rb +19 -0
- data/test/unit/cardinality_test.rb +56 -0
- data/test/unit/central_test.rb +65 -0
- data/test/unit/change_state_side_effect_test.rb +41 -0
- data/test/unit/class_method_test.rb +295 -0
- data/test/unit/configuration_test.rb +38 -0
- data/test/unit/date_time_inspect_test.rb +21 -0
- data/test/unit/exception_raiser_test.rb +42 -0
- data/test/unit/expectation_list_test.rb +57 -0
- data/test/unit/expectation_test.rb +480 -0
- data/test/unit/hash_inspect_test.rb +16 -0
- data/test/unit/in_state_ordering_constraint_test.rb +43 -0
- data/test/unit/metaclass_test.rb +22 -0
- data/test/unit/method_matcher_test.rb +23 -0
- data/test/unit/mock_test.rb +302 -0
- data/test/unit/mockery_test.rb +149 -0
- data/test/unit/multiple_yields_test.rb +18 -0
- data/test/unit/no_yields_test.rb +18 -0
- data/test/unit/object_inspect_test.rb +37 -0
- data/test/unit/object_test.rb +82 -0
- data/test/unit/parameter_matchers/all_of_test.rb +26 -0
- data/test/unit/parameter_matchers/any_of_test.rb +26 -0
- data/test/unit/parameter_matchers/anything_test.rb +21 -0
- data/test/unit/parameter_matchers/equals_test.rb +25 -0
- data/test/unit/parameter_matchers/has_entries_test.rb +51 -0
- data/test/unit/parameter_matchers/has_entry_test.rb +82 -0
- data/test/unit/parameter_matchers/has_key_test.rb +55 -0
- data/test/unit/parameter_matchers/has_value_test.rb +57 -0
- data/test/unit/parameter_matchers/includes_test.rb +44 -0
- data/test/unit/parameter_matchers/instance_of_test.rb +25 -0
- data/test/unit/parameter_matchers/is_a_test.rb +25 -0
- data/test/unit/parameter_matchers/kind_of_test.rb +25 -0
- data/test/unit/parameter_matchers/not_test.rb +26 -0
- data/test/unit/parameter_matchers/regexp_matches_test.rb +46 -0
- data/test/unit/parameter_matchers/responds_with_test.rb +25 -0
- data/test/unit/parameter_matchers/stub_matcher.rb +27 -0
- data/test/unit/parameter_matchers/yaml_equivalent_test.rb +25 -0
- data/test/unit/parameters_matcher_test.rb +121 -0
- data/test/unit/return_values_test.rb +63 -0
- data/test/unit/sequence_test.rb +104 -0
- data/test/unit/single_return_value_test.rb +14 -0
- data/test/unit/single_yield_test.rb +18 -0
- data/test/unit/state_machine_test.rb +98 -0
- data/test/unit/string_inspect_test.rb +11 -0
- data/test/unit/yield_parameters_test.rb +93 -0
- metadata +240 -0
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'mocha/backtrace_filter'
|
2
|
+
|
3
|
+
module Mocha # :nodoc:
|
4
|
+
|
5
|
+
# Exception raised when an action prevented by Configuration#prevent is attempted.
|
6
|
+
class StubbingError < StandardError
|
7
|
+
|
8
|
+
def initialize(message = nil, backtrace = []) # :nodoc:
|
9
|
+
super(message)
|
10
|
+
filter = BacktraceFilter.new
|
11
|
+
set_backtrace(filter.filtered(backtrace))
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,18 @@
|
|
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
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'mocha/no_yields'
|
2
|
+
require 'mocha/single_yield'
|
3
|
+
require 'mocha/multiple_yields'
|
4
|
+
|
5
|
+
module Mocha # :nodoc:
|
6
|
+
|
7
|
+
class YieldParameters # :nodoc:
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@parameter_groups = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def next_invocation
|
14
|
+
case @parameter_groups.length
|
15
|
+
when 0 then NoYields.new
|
16
|
+
when 1 then @parameter_groups.first
|
17
|
+
else @parameter_groups.shift
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def add(*parameters)
|
22
|
+
@parameter_groups << SingleYield.new(*parameters)
|
23
|
+
end
|
24
|
+
|
25
|
+
def multiple_add(*parameter_groups)
|
26
|
+
@parameter_groups << MultipleYields.new(*parameter_groups)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/lib/stubba.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
|
+
require 'test_runner'
|
3
|
+
require 'mocha/configuration'
|
4
|
+
|
5
|
+
module AcceptanceTest
|
6
|
+
|
7
|
+
class FakeLogger
|
8
|
+
|
9
|
+
attr_reader :warnings
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@warnings = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def warn(message)
|
16
|
+
@warnings << message
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :logger
|
22
|
+
|
23
|
+
include TestRunner
|
24
|
+
|
25
|
+
def setup_acceptance_test
|
26
|
+
Mocha::Configuration.reset_configuration
|
27
|
+
@logger = FakeLogger.new
|
28
|
+
mockery = Mocha::Mockery.instance
|
29
|
+
@original_logger = mockery.logger
|
30
|
+
mockery.logger = @logger
|
31
|
+
end
|
32
|
+
|
33
|
+
def teardown_acceptance_test
|
34
|
+
Mocha::Configuration.reset_configuration
|
35
|
+
Mocha::Mockery.instance.logger = @original_logger
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "acceptance_test_helper")
|
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
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "acceptance_test_helper")
|
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
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "acceptance_test_helper")
|
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
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "acceptance_test_helper")
|
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
|
@@ -0,0 +1,196 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "acceptance_test_helper")
|
2
|
+
require 'mocha'
|
3
|
+
|
4
|
+
class ExpectedInvocationCountTest < 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_pass_if_method_is_never_expected_and_is_never_called
|
17
|
+
test_result = run_as_test do
|
18
|
+
mock = mock('mock')
|
19
|
+
mock.expects(:method).never
|
20
|
+
0.times { mock.method }
|
21
|
+
end
|
22
|
+
assert_passed(test_result)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_should_fail_fast_if_method_is_never_expected_but_is_called_once
|
26
|
+
test_result = run_as_test do
|
27
|
+
mock = mock('mock')
|
28
|
+
mock.expects(:method).never
|
29
|
+
1.times { mock.method }
|
30
|
+
end
|
31
|
+
assert_failed(test_result)
|
32
|
+
assert_equal ["unexpected invocation: #<Mock:mock>.method()\nsatisfied expectations:\n- expected never, not yet invoked: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_should_pass_if_method_is_expected_twice_and_is_called_twice
|
36
|
+
test_result = run_as_test do
|
37
|
+
mock = mock('mock')
|
38
|
+
mock.expects(:method).twice
|
39
|
+
2.times { mock.method }
|
40
|
+
end
|
41
|
+
assert_passed(test_result)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_should_fail_if_method_is_expected_twice_but_is_called_once
|
45
|
+
test_result = run_as_test do
|
46
|
+
mock = mock('mock')
|
47
|
+
mock.expects(:method).twice
|
48
|
+
1.times { mock.method }
|
49
|
+
end
|
50
|
+
assert_failed(test_result)
|
51
|
+
assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected exactly twice, already invoked once: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_should_fail_fast_if_method_is_expected_twice_but_is_called_three_times
|
55
|
+
test_result = run_as_test do
|
56
|
+
mock = mock('mock')
|
57
|
+
mock.expects(:method).twice
|
58
|
+
3.times { mock.method }
|
59
|
+
end
|
60
|
+
assert_failed(test_result)
|
61
|
+
assert_equal ["unexpected invocation: #<Mock:mock>.method()\nsatisfied expectations:\n- expected exactly twice, already invoked twice: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_should_pass_if_method_is_expected_between_two_and_four_times_and_is_called_twice
|
65
|
+
test_result = run_as_test do
|
66
|
+
mock = mock('mock')
|
67
|
+
mock.expects(:method).times(2..4)
|
68
|
+
2.times { mock.method }
|
69
|
+
end
|
70
|
+
assert_passed(test_result)
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_should_pass_if_method_is_expected_between_two_and_four_times_and_is_called_three_times
|
74
|
+
test_result = run_as_test do
|
75
|
+
mock = mock('mock')
|
76
|
+
mock.expects(:method).times(2..4)
|
77
|
+
3.times { mock.method }
|
78
|
+
end
|
79
|
+
assert_passed(test_result)
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_should_pass_if_method_is_expected_between_two_and_four_times_and_is_called_four_times
|
83
|
+
test_result = run_as_test do
|
84
|
+
mock = mock('mock')
|
85
|
+
mock.expects(:method).times(2..4)
|
86
|
+
4.times { mock.method }
|
87
|
+
end
|
88
|
+
assert_passed(test_result)
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_should_fail_if_method_is_expected_between_two_and_four_times_and_is_called_once
|
92
|
+
test_result = run_as_test do
|
93
|
+
mock = mock('mock')
|
94
|
+
mock.expects(:method).times(2..4)
|
95
|
+
1.times { mock.method }
|
96
|
+
end
|
97
|
+
assert_failed(test_result)
|
98
|
+
assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected between 2 and 4 times, already invoked once: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_should_fail_fast_if_method_is_expected_between_two_and_four_times_and_is_called_five_times
|
102
|
+
test_result = run_as_test do
|
103
|
+
mock = mock('mock')
|
104
|
+
mock.expects(:method).times(2..4)
|
105
|
+
5.times { mock.method }
|
106
|
+
end
|
107
|
+
assert_failed(test_result)
|
108
|
+
assert_equal ["unexpected invocation: #<Mock:mock>.method()\nsatisfied expectations:\n- expected between 2 and 4 times, already invoked 4 times: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_should_pass_if_method_is_expected_at_least_once_and_is_called_once
|
112
|
+
test_result = run_as_test do
|
113
|
+
mock = mock('mock')
|
114
|
+
mock.expects(:method).at_least_once
|
115
|
+
1.times { mock.method }
|
116
|
+
end
|
117
|
+
assert_passed(test_result)
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_should_pass_if_method_is_expected_at_least_once_and_is_called_twice
|
121
|
+
test_result = run_as_test do
|
122
|
+
mock = mock('mock')
|
123
|
+
mock.expects(:method).at_least_once
|
124
|
+
2.times { mock.method }
|
125
|
+
end
|
126
|
+
assert_passed(test_result)
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_should_fail_if_method_is_expected_at_least_once_but_is_never_called
|
130
|
+
test_result = run_as_test do
|
131
|
+
mock = mock('mock')
|
132
|
+
mock.expects(:method).at_least_once
|
133
|
+
0.times { mock.method }
|
134
|
+
end
|
135
|
+
assert_failed(test_result)
|
136
|
+
assert_equal ["not all expectations were satisfied\nunsatisfied expectations:\n- expected at least once, not yet invoked: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_should_pass_if_method_is_expected_at_most_once_and_is_never_called
|
140
|
+
test_result = run_as_test do
|
141
|
+
mock = mock('mock')
|
142
|
+
mock.expects(:method).at_most_once
|
143
|
+
0.times { mock.method }
|
144
|
+
end
|
145
|
+
assert_passed(test_result)
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_should_pass_if_method_is_expected_at_most_once_and_called_once
|
149
|
+
test_result = run_as_test do
|
150
|
+
mock = mock('mock')
|
151
|
+
mock.expects(:method).at_most_once
|
152
|
+
1.times { mock.method }
|
153
|
+
end
|
154
|
+
assert_passed(test_result)
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_should_fail_fast_if_method_is_expected_at_most_once_but_is_called_twice
|
158
|
+
test_result = run_as_test do
|
159
|
+
mock = mock('mock')
|
160
|
+
mock.expects(:method).at_most_once
|
161
|
+
2.times { mock.method }
|
162
|
+
end
|
163
|
+
assert_failed(test_result)
|
164
|
+
assert_equal ["unexpected invocation: #<Mock:mock>.method()\nsatisfied expectations:\n- expected at most once, already invoked once: #<Mock:mock>.method(any_parameters)\n"], test_result.failure_messages
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_should_pass_if_method_is_never_expected_and_is_never_called_even_if_everything_is_stubbed
|
168
|
+
test_result = run_as_test do
|
169
|
+
stub = stub_everything('stub')
|
170
|
+
stub.expects(:method).never
|
171
|
+
0.times { stub.method }
|
172
|
+
end
|
173
|
+
assert_passed(test_result)
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_should_fail_fast_if_method_is_never_expected_but_is_called_once_even_if_everything_is_stubbed
|
177
|
+
test_result = run_as_test do
|
178
|
+
stub = stub_everything('stub')
|
179
|
+
stub.expects(:method).never
|
180
|
+
1.times { stub.method }
|
181
|
+
end
|
182
|
+
assert_failed(test_result)
|
183
|
+
assert_equal ["unexpected invocation: #<Mock:stub>.method()\nsatisfied expectations:\n- expected never, not yet invoked: #<Mock:stub>.method(any_parameters)\n"], test_result.failure_messages
|
184
|
+
end
|
185
|
+
|
186
|
+
def test_should_fail_fast_if_there_is_no_matching_expectation
|
187
|
+
test_result = run_as_test do
|
188
|
+
mock = mock('mock')
|
189
|
+
mock.expects(:method).with(1)
|
190
|
+
1.times { mock.method }
|
191
|
+
end
|
192
|
+
assert_failed(test_result)
|
193
|
+
assert_equal ["unexpected invocation: #<Mock:mock>.method()\nunsatisfied expectations:\n- expected exactly once, not yet invoked: #<Mock:mock>.method(1)\n"], test_result.failure_messages
|
194
|
+
end
|
195
|
+
|
196
|
+
end
|