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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
|
"""Tests for flexmock spying feature."""
# pylint: disable=missing-docstring,no-member,broad-exception-raised
import re
from flexmock import exceptions, flexmock
from flexmock._api import flexmock_teardown
from tests.utils import assert_raises
class SpyingTestCase:
def test_spying_non_existent_mock_object_method_should_fail(self):
mock = flexmock()
with assert_raises(
exceptions.FlexmockError,
"Mock object does not have attribute 'method_foo'. "
'Did you mean to call should_receive("method_foo") instead?',
):
mock.should_call("method_foo")
mock = flexmock(method_foo=lambda: "ok")
mock.should_call("method_foo")
def test_pass_thru_should_call_original_method_only_once(self):
class Nyan:
def __init__(self):
self.value = 0
def method(self):
self.value += 1
obj = Nyan()
flexmock(obj)
obj.should_call("method")
obj.method()
assert obj.value == 1
def test_should_call_works_for_same_method_with_different_args(self):
class Foo:
def method(self, arg):
pass
instance = Foo()
flexmock(instance).should_call("method").with_args("foo").once()
flexmock(instance).should_call("method").with_args("bar").once()
instance.method("foo")
instance.method("bar")
flexmock_teardown()
def test_should_call_fails_properly_for_same_method_with_different_args(self):
class Foo:
def method(self, arg):
pass
instance = Foo()
flexmock(instance).should_call("method").with_args("foo").once()
flexmock(instance).should_call("method").with_args("bar").once()
instance.method("foo")
with assert_raises(
exceptions.MethodCallError,
'method(arg="bar") expected to be called exactly 1 time, called 0 times',
):
flexmock_teardown()
def test_should_call_alias_should_create_a_spy(self):
class Foo:
def get_stuff(self):
return "yay"
instance = Foo()
flexmock(instance).should_call("get_stuff").and_return("yay").once()
with assert_raises(
exceptions.MethodCallError,
"get_stuff() expected to be called exactly 1 time, called 0 times",
):
flexmock_teardown()
def test_spy_should_not_match_falsy_stuff(self):
class Foo:
def method1(self):
return None
def method2(self):
return False
def method3(self):
return []
def method4(self):
return ""
instance = Foo()
flexmock(instance).should_call("method1").and_return("bar").once()
flexmock(instance).should_call("method2").and_return("bar").once()
flexmock(instance).should_call("method3").and_return("bar").once()
flexmock(instance).should_call("method4").and_return("bar").once()
with assert_raises(
exceptions.FlexmockError,
(
"Returned values for call method1 did not match expectation:\n"
" Expected:\t'bar'\n"
" Returned:\tNone"
),
):
instance.method1()
with assert_raises(
exceptions.FlexmockError,
(
"Returned values for call method2 did not match expectation:\n"
" Expected:\t'bar'\n"
" Returned:\tFalse"
),
):
instance.method2()
with assert_raises(
exceptions.FlexmockError,
(
"Returned values for call method3 did not match expectation:\n"
" Expected:\t'bar'\n"
" Returned:\t[]"
),
):
instance.method3()
with assert_raises(
exceptions.FlexmockError,
(
"Returned values for call method4 did not match expectation:\n"
" Expected:\t'bar'\n"
" Returned:\t''"
),
):
instance.method4()
def test_spy_should_match_return_value_class(self):
class User:
pass
user = User()
instance = flexmock(
foo=lambda: ("bar", "baz"),
bar=lambda: user,
baz=lambda: None,
bax=lambda: None,
)
instance.should_call("foo").and_return(str, str)
instance.should_call("bar").and_return(User)
instance.should_call("baz").and_return(object)
instance.should_call("bax").and_return(None)
assert instance.foo() == ("bar", "baz")
assert instance.bar() == user
assert instance.baz() is None
assert instance.bax() is None
def test_flexmock_spy_should_not_clobber_original_method(self):
class User:
def get_stuff(self):
return "real", "stuff"
user = User()
flexmock(user).should_call("get_stuff")
flexmock(user).should_call("get_stuff")
assert user.get_stuff() == ("real", "stuff")
def test_and_raise_with_value_that_is_not_a_class(self):
class RaisesException:
def get_stuff(self):
raise RuntimeError("baz")
instance = RaisesException()
flexmock(instance).should_call("get_stuff").and_raise(RuntimeError("foo")).once()
with assert_raises(
exceptions.ExceptionClassError,
re.compile(
r"Raised exception for call get_stuff did not match expectation:\n"
# Python 3.6 contains comma after 'foo'
r" Expected:\tRuntimeError\('foo',?\)\n"
r" Raised:\t<class 'RuntimeError'>\n\n"
r"Did you try to call and_raise with an instance\?\n"
r'Instead of and_raise\(Exception\("arg"\)\), try and_raise\(Exception, "arg"\)',
),
):
instance.get_stuff()
def test_flexmock_should_blow_up_on_wrong_spy_return_values(self):
class User:
def get_stuff(self):
return "real", "stuff"
def get_more_stuff(self):
return "other", "stuff"
user = User()
flexmock(user).should_call("get_stuff").and_return("other", "stuff")
with assert_raises(
exceptions.MethodSignatureError,
(
"Returned values for call get_stuff did not match expectation:\n"
" Expected:\t('other', 'stuff')\n"
" Returned:\t('real', 'stuff')"
),
):
user.get_stuff()
flexmock(user).should_call("get_more_stuff").and_return()
with assert_raises(
exceptions.MethodSignatureError,
(
"Returned values for call get_more_stuff did not match expectation:\n"
" Expected:\tNone\n"
" Returned:\t('other', 'stuff')"
),
):
user.get_more_stuff()
def test_flexmock_should_reraise_exception_in_spy_with_return_values(self):
class RaisesException:
@classmethod
def class_method(cls):
raise RuntimeError("foo")
@staticmethod
def static_method():
raise RuntimeError("bar")
def instance_method(self):
raise RuntimeError("baz")
instance = RaisesException()
flexmock(instance).should_call("class_method").and_return("foo").once()
flexmock(instance).should_call("static_method").and_return("bar").once()
flexmock(instance).should_call("instance_method").and_return("baz").once()
with assert_raises(RuntimeError, "foo"):
instance.class_method()
with assert_raises(RuntimeError, "bar"):
instance.static_method()
with assert_raises(RuntimeError, "baz"):
instance.instance_method()
flexmock(RaisesException).should_call("class_method").once()
flexmock(RaisesException).should_call("static_method").once()
with assert_raises(RuntimeError, "foo"):
RaisesException.class_method()
with assert_raises(RuntimeError, "bar"):
RaisesException.static_method()
def test_flexmock_should_reraise_exception_in_spy(self):
class RaisesException:
@classmethod
def class_method(cls):
raise RuntimeError("foo")
@staticmethod
def static_method():
raise RuntimeError("bar")
def instance_method(self):
raise RuntimeError("baz")
instance = RaisesException()
flexmock(instance).should_call("class_method").once()
flexmock(instance).should_call("static_method").once()
flexmock(instance).should_call("instance_method").once()
with assert_raises(RuntimeError, "foo"):
instance.class_method()
with assert_raises(RuntimeError, "bar"):
instance.static_method()
with assert_raises(RuntimeError, "baz"):
instance.instance_method()
flexmock(RaisesException).should_call("class_method").once()
flexmock(RaisesException).should_call("static_method").once()
with assert_raises(RuntimeError, "foo"):
RaisesException.class_method()
with assert_raises(RuntimeError, "bar"):
RaisesException.static_method()
def test_flexmock_should_verify_correct_spy_return_values(self):
class User:
def get_stuff(self):
return "real", "stuff"
user = User()
flexmock(user).should_call("get_stuff").and_return("real", "stuff")
assert user.get_stuff() == ("real", "stuff")
def test_flexmock_should_verify_correct_spy_regexp_return_values(self):
class User:
def get_stuff(self):
return "real", "stuff"
user = User()
flexmock(user).should_call("get_stuff").and_return(
re.compile("ea.*"), re.compile("^stuff$")
)
assert user.get_stuff() == ("real", "stuff")
def test_flexmock_should_verify_spy_raises_correct_exception_class(self):
class FakeException(Exception):
def __init__(self, param, param2):
self.message = f"{param}, {param2}"
Exception.__init__(self)
class User:
def get_stuff(self):
raise FakeException(1, 2)
user = User()
flexmock(user).should_call("get_stuff").and_raise(FakeException, 1, 2)
user.get_stuff()
def test_flexmock_should_verify_spy_matches_exception_message(self):
class FakeException(Exception):
def __init__(self, param1, param2):
self.param1 = param1
self.param2 = param2
Exception.__init__(self, param1)
def __str__(self):
return f"{self.param1}, {self.param2}"
class User:
def get_stuff(self):
raise FakeException("1", "2")
user = User()
flexmock(user).should_call("get_stuff").and_raise(FakeException, "2", "1")
with assert_raises(
exceptions.ExceptionMessageError,
(
"Error message mismatch with raised FakeException:\n"
" Expected message:\n\t'1, 2'\n"
" Received message:\n\t'2, 1'"
),
):
user.get_stuff()
def test_flexmock_should_verify_spy_matches_exception_regexp(self):
class User:
def get_stuff(self):
raise Exception("123asdf345")
user = User()
flexmock(user).should_call("get_stuff").and_raise(Exception, re.compile("asdf"))
user.get_stuff()
flexmock_teardown()
def test_flexmock_should_verify_spy_matches_exception_regexp_mismatch(self):
class User:
def get_stuff(self):
raise Exception("123asdf345")
user = User()
flexmock(user).should_call("get_stuff").and_raise(Exception, re.compile("^asdf"))
with assert_raises(
exceptions.ExceptionMessageError,
(
"Error message mismatch with raised Exception:\n"
" Expected pattern:\n\t/^asdf/\n"
" Received message:\n\t'123asdf345'"
),
):
user.get_stuff()
def test_flexmock_should_blow_up_on_wrong_spy_exception_type(self):
class User:
def get_stuff(self):
raise exceptions.CallOrderError("foo")
user = User()
flexmock(user).should_call("get_stuff").and_raise(exceptions.MethodCallError)
with assert_raises(
exceptions.ExceptionClassError,
(
"Raised exception for call get_stuff did not match expectation:\n"
" Expected:\t<class 'flexmock.exceptions.MethodCallError'>\n"
" Raised:\t<class 'flexmock.exceptions.CallOrderError'>"
),
):
user.get_stuff()
def test_flexmock_should_match_spy_exception_parent_type(self):
class User:
def get_stuff(self):
raise exceptions.CallOrderError("foo")
user = User()
flexmock(user).should_call("get_stuff").and_raise(exceptions.FlexmockError)
user.get_stuff()
def test_flexmock_should_call_respects_matched_expectations(self):
class Group:
def method1(self, arg1, arg2="b"):
return f"{arg1}:{arg2}"
def method2(self, arg):
return arg
group = Group()
flexmock(group).should_call("method1").twice()
assert group.method1("a", arg2="c") == "a:c"
assert group.method1("a") == "a:b"
group.should_call("method2").once().with_args("c")
assert group.method2("c") == "c"
flexmock_teardown()
def test_flexmock_should_call_respects_unmatched_expectations(self):
class Group:
def method1(self, arg1, arg2="b"):
return f"{arg1}:{arg2}"
def method2(self, arg1):
pass
group = Group()
flexmock(group).should_call("method1").at_least().once()
with assert_raises(
exceptions.MethodCallError,
"method1() expected to be called at least 1 time, called 0 times",
):
flexmock_teardown()
flexmock(group)
group.should_call("method2").with_args("a").once()
group.should_receive("method2").with_args("not a")
group.method2("not a")
with assert_raises(
exceptions.MethodCallError,
'method2(arg1="a") expected to be called exactly 1 time, called 0 times',
):
flexmock_teardown()
def test_flexmock_should_not_blow_up_on_should_call_for_class_methods(self):
class User:
@classmethod
def method(cls):
return "class"
flexmock(User).should_call("method")
assert User.method() == "class"
def test_flexmock_should_not_blow_up_on_should_call_for_static_methods(self):
class User:
@staticmethod
def method():
return "static"
flexmock(User).should_call("method")
assert User.method() == "static"
def test_should_call_with_class_default_attributes(self):
"""Flexmock should not allow mocking class default attributes like
__call__ on an instance.
"""
class WithCall:
def __call__(self, arg1):
return arg1
instance = WithCall()
with assert_raises(
exceptions.FlexmockError,
re.compile(r".+<locals>\.WithCall object at 0x.+> does not have attribute '__call__'"),
):
flexmock(instance).should_call("__call__")
def test_should_call_on_class_mock(self):
class User:
def __init__(self):
self.value = "value"
def method1(self):
return "class"
def method2(self):
return self.value
# Access class-level method
user1 = User()
user2 = User()
flexmock(User).should_call("method1").once()
with assert_raises(
exceptions.MethodCallError,
"method1() expected to be called exactly 1 time, called 0 times",
):
flexmock_teardown()
flexmock(User).should_call("method1").twice()
assert user1.method1() == "class"
assert user2.method1() == "class"
# Access instance attributes
flexmock(User).should_call("method2").once()
with assert_raises(
exceptions.MethodCallError,
"method2() expected to be called exactly 1 time, called 0 times",
):
flexmock_teardown()
flexmock(User).should_call("method2").twice()
assert user1.method2() == "value"
assert user2.method2() == "value"
# Try resetting the expectation
flexmock(User).should_call("method2").once()
assert user1.method2() == "value"
|