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
|
"""Tests for mocking and spying derived classes."""
# pylint: disable=missing-docstring,no-member
from flexmock import exceptions, flexmock
from flexmock._api import flexmock_teardown
from tests.some_module import DerivedClass, SomeClass
from tests.utils import assert_raises
class DerivedTestCase:
def test_mock_class_method_on_derived_class(self):
flexmock(DerivedClass).should_receive("class_method").and_return(2).twice()
assert DerivedClass().class_method() == 2
assert DerivedClass.class_method() == 2
def test_mock_class_method_on_derived_class_after_mocking_base_class(self):
flexmock(SomeClass).should_receive("class_method").and_return(1).once()
assert SomeClass.class_method() == 1
flexmock(DerivedClass).should_receive("class_method").and_return(2).twice()
assert DerivedClass().class_method() == 2
assert DerivedClass.class_method() == 2
def test_mock_static_method_on_derived_class(self):
flexmock(DerivedClass).should_receive("static_method").and_return(4).twice()
assert DerivedClass().static_method() == 4
assert DerivedClass.static_method() == 4
def test_mock_static_method_on_derived_class_after_mocking_base_class(self):
flexmock(SomeClass).should_receive("static_method").and_return(3).once()
assert SomeClass.static_method() == 3
flexmock(DerivedClass).should_receive("static_method").and_return(4).twice()
assert DerivedClass().static_method() == 4
assert DerivedClass.static_method() == 4
def test_mock_class_method_with_args_on_derived_class(self):
flexmock(DerivedClass).should_receive("class_method_with_args").with_args(2).and_return(
3
).twice()
assert DerivedClass().class_method_with_args(2) == 3
assert DerivedClass.class_method_with_args(2) == 3
def test_mock_class_method_with_args_on_derived_class_after_mocking_base_class(self):
flexmock(SomeClass).should_receive("class_method_with_args").with_args(1).and_return(
2
).once()
assert SomeClass.class_method_with_args(1) == 2
flexmock(DerivedClass).should_receive("class_method_with_args").with_args(2).and_return(
3
).twice()
assert DerivedClass().class_method_with_args(2) == 3
assert DerivedClass.class_method_with_args(2) == 3
def test_mock_static_method_with_args_on_derived_class(self):
flexmock(DerivedClass).should_receive("static_method_with_args").with_args(4).and_return(
5
).twice()
assert DerivedClass().static_method_with_args(4) == 5
assert DerivedClass.static_method_with_args(4) == 5
def test_mock_static_method_with_args_on_derived_class_after_mocking_base_class(self):
flexmock(SomeClass).should_receive("static_method_with_args").with_args(2).and_return(
3
).once()
assert SomeClass.static_method_with_args(2) == 3
flexmock(DerivedClass).should_receive("static_method_with_args").with_args(4).and_return(
5
).twice()
assert DerivedClass().static_method_with_args(4) == 5
assert DerivedClass.static_method_with_args(4) == 5
def test_spy_class_method_on_derived_class(self):
flexmock(DerivedClass).should_call("class_method").and_return("class_method").twice()
assert DerivedClass().class_method() == "class_method"
assert DerivedClass.class_method() == "class_method"
def test_spy_class_method_on_derived_class_after_spying_base_class(self):
flexmock(SomeClass).should_call("class_method").and_return("class_method").times(
3
) # TODO: Should be once #80
assert SomeClass.class_method() == "class_method"
flexmock(DerivedClass).should_call("class_method").and_return("class_method").twice()
assert DerivedClass().class_method() == "class_method"
assert DerivedClass.class_method() == "class_method"
def test_spy_static_method_on_derived_class(self):
flexmock(DerivedClass).should_call("static_method").and_return("static_method").twice()
assert DerivedClass().static_method() == "static_method"
assert DerivedClass.static_method() == "static_method"
def test_spy_static_method_on_derived_class_after_spying_base_class(self):
flexmock(SomeClass).should_call("static_method").and_return("static_method").times(
3
) # TODO: Should be once #80
assert SomeClass.static_method() == "static_method"
flexmock(DerivedClass).should_call("static_method").and_return("static_method").twice()
assert DerivedClass().static_method() == "static_method"
assert DerivedClass.static_method() == "static_method"
def test_spy_class_method_with_args_on_derived_class(self):
flexmock(DerivedClass).should_call("class_method_with_args").with_args(2).and_return(2)
assert DerivedClass().class_method_with_args(2) == 2
assert DerivedClass.class_method_with_args(2) == 2
@assert_raises(
exceptions.MethodSignatureError, match=None
) # TODO: Should not raise exception #79
def test_spy_class_method_with_args_on_derived_class_after_spying_base_class(self):
flexmock(SomeClass).should_call("class_method_with_args").with_args(1).and_return(1)
assert SomeClass.class_method_with_args(1) == 1
flexmock(DerivedClass).should_call("class_method_with_args").with_args(2).and_return(2)
assert DerivedClass().class_method_with_args(2) == 2
assert DerivedClass.class_method_with_args(2) == 2
def test_spy_static_method_with_args_on_derived_class(self):
flexmock(DerivedClass).should_call("static_method_with_args").with_args(4).and_return(
4
).twice()
assert DerivedClass().static_method_with_args(4) == 4
assert DerivedClass.static_method_with_args(4) == 4
@assert_raises(
exceptions.MethodSignatureError, match=None
) # TODO: Should not raise exception #79
def test_spy_static_method_with_args_on_derived_class_after_spying_base_class(self):
flexmock(SomeClass).should_call("static_method_with_args").with_args(2).and_return(2).once()
assert SomeClass.static_method_with_args(2) == 2
flexmock(DerivedClass).should_call("static_method_with_args").with_args(4).and_return(
4
).once() # should be twice
assert DerivedClass().static_method_with_args(4) == 4
assert DerivedClass.static_method_with_args(4) == 4
def test_multi_mock_class_method_on_derived_class_is_properly_torn_down(self):
flexmock(DerivedClass).should_receive("class_method_with_args").with_args(1).once()
DerivedClass.class_method_with_args(1)
flexmock(DerivedClass).should_receive("class_method_with_args").with_args(2).once()
DerivedClass.class_method_with_args(2)
flexmock(DerivedClass).should_receive("class_method_with_args").with_args(3).once()
DerivedClass.class_method_with_args(3)
# Run teardown manually to allow this test case to be contained in single test
flexmock_teardown()
flexmock(DerivedClass).should_receive("class_method_with_args").with_args(4).once()
DerivedClass.class_method_with_args(4)
|