1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
require File.expand_path('../acceptance_test_helper', __FILE__)
class ExpectedInvocationCountTest < Mocha::TestCase
include AcceptanceTest
def setup
setup_acceptance_test
end
def teardown
teardown_acceptance_test
end
def test_should_pass_if_method_is_never_expected_and_is_never_called
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).never
0.times { mock.method }
end
assert_passed(test_result)
end
def test_should_fail_fast_if_method_is_never_expected_but_is_called_once
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).never
1.times { mock.method }
end
assert_failed(test_result)
assert_equal [
'unexpected invocation: #<Mock:mock>.method()',
'unsatisfied expectations:',
'- expected never, invoked once: #<Mock:mock>.method(any_parameters)'
], test_result.failure_message_lines
end
def test_should_pass_if_method_is_expected_twice_and_is_called_twice
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).twice
2.times { mock.method }
end
assert_passed(test_result)
end
def test_should_fail_if_method_is_expected_twice_but_is_called_once
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).twice
1.times { mock.method }
end
assert_failed(test_result)
assert_equal [
'not all expectations were satisfied',
'unsatisfied expectations:',
'- expected exactly twice, invoked once: #<Mock:mock>.method(any_parameters)'
], test_result.failure_message_lines
end
def test_should_fail_fast_if_method_is_expected_twice_but_is_called_three_times
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).twice
3.times { mock.method }
end
assert_failed(test_result)
assert_equal [
'unexpected invocation: #<Mock:mock>.method()',
'unsatisfied expectations:',
'- expected exactly twice, invoked 3 times: #<Mock:mock>.method(any_parameters)'
], test_result.failure_message_lines
end
def test_should_pass_if_method_is_expected_between_two_and_four_times_and_is_called_twice
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).times(2..4)
2.times { mock.method }
end
assert_passed(test_result)
end
def test_should_pass_if_method_is_expected_between_two_and_four_times_and_is_called_three_times
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).times(2..4)
3.times { mock.method }
end
assert_passed(test_result)
end
def test_should_pass_if_method_is_expected_between_two_and_four_times_and_is_called_four_times
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).times(2..4)
4.times { mock.method }
end
assert_passed(test_result)
end
def test_should_fail_if_method_is_expected_between_two_and_four_times_and_is_called_once
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).times(2..4)
1.times { mock.method }
end
assert_failed(test_result)
assert_equal [
'not all expectations were satisfied',
'unsatisfied expectations:',
'- expected between 2 and 4 times, invoked once: #<Mock:mock>.method(any_parameters)'
], test_result.failure_message_lines
end
def test_should_fail_fast_if_method_is_expected_between_two_and_four_times_and_is_called_five_times
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).times(2..4)
5.times { mock.method }
end
assert_failed(test_result)
assert_equal [
'unexpected invocation: #<Mock:mock>.method()',
'unsatisfied expectations:',
'- expected between 2 and 4 times, invoked 5 times: #<Mock:mock>.method(any_parameters)'
], test_result.failure_message_lines
end
def test_should_pass_if_method_is_expected_at_least_once_and_is_called_once
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).at_least_once
1.times { mock.method }
end
assert_passed(test_result)
end
def test_should_pass_if_method_is_expected_at_least_once_and_is_called_twice
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).at_least_once
2.times { mock.method }
end
assert_passed(test_result)
end
def test_should_fail_if_method_is_expected_at_least_once_but_is_never_called
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).at_least_once
0.times { mock.method }
end
assert_failed(test_result)
assert_equal [
'not all expectations were satisfied',
'unsatisfied expectations:',
'- expected at least once, invoked never: #<Mock:mock>.method(any_parameters)'
], test_result.failure_message_lines
end
def test_should_pass_if_method_is_expected_at_most_once_and_is_never_called
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).at_most_once
0.times { mock.method }
end
assert_passed(test_result)
end
def test_should_pass_if_method_is_expected_at_most_once_and_called_once
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).at_most_once
1.times { mock.method }
end
assert_passed(test_result)
end
def test_should_fail_fast_if_method_is_expected_at_most_once_but_is_called_twice
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).at_most_once
2.times { mock.method }
end
assert_failed(test_result)
assert_equal [
'unexpected invocation: #<Mock:mock>.method()',
'unsatisfied expectations:',
'- expected at most once, invoked twice: #<Mock:mock>.method(any_parameters)'
], test_result.failure_message_lines
end
def test_should_pass_if_method_is_never_expected_and_is_never_called_even_if_everything_is_stubbed
test_result = run_as_test do
stub = stub_everything('stub')
stub.expects(:method).never
0.times { stub.method }
end
assert_passed(test_result)
end
def test_should_fail_fast_if_method_is_never_expected_but_is_called_once_even_if_everything_is_stubbed
test_result = run_as_test do
stub = stub_everything('stub')
stub.expects(:method).never
1.times { stub.method }
end
assert_failed(test_result)
assert_equal [
'unexpected invocation: #<Mock:stub>.method()',
'unsatisfied expectations:',
'- expected never, invoked once: #<Mock:stub>.method(any_parameters)'
], test_result.failure_message_lines
end
def test_should_fail_fast_if_there_is_no_matching_expectation
test_result = run_as_test do
mock = mock('mock')
mock.expects(:method).with(1)
1.times { mock.method }
end
assert_failed(test_result)
assert_equal [
'unexpected invocation: #<Mock:mock>.method()',
'unsatisfied expectations:',
'- expected exactly once, invoked never: #<Mock:mock>.method(1)'
], test_result.failure_message_lines
end
def test_should_pass_if_cardinality_is_satisfied_using_calls_made_both_before_and_after_updating_cardinality
test_result = run_as_test do
mock = mock('mock')
expectation = mock.expects(:method)
mock.method
expectation.twice
mock.method
end
assert_passed(test_result)
end
end
|