aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_helpers.py
diff options
context:
space:
mode:
authorGravatar Sebastiaan Zeeff <[email protected]>2019-10-28 15:56:34 +0100
committerGravatar Sebastiaan Zeeff <[email protected]>2019-10-28 15:56:34 +0100
commitc5d0eb473a9a1dc486dd2dd60603463435e49da4 (patch)
treee130e17a17bd040f74b0dd275ef70bee033ae391 /tests/test_helpers.py
parentMerge pull request #527 from kraktus/compact_free (diff)
Change generation of child mocks
- https://docs.python.org/3/library/unittest.mock.html We previously used an override of the `__new__` method to prevent our custom mock types from instantiating their children with their own type instead of a general mock type like `MagicMock` or `Mock`. As it turns out, the Python documentation suggests another method of doing this that does not involve overriding `__new__`. This commit implements this new method to make sure we're using the idiomatic way of handling this. The suggested method is overriding the `_get_child_mock` method in the subclass. To make our code DRY, I've created a mixin that should come BEFORE the mock type we're subclassing in the MRO. --- In addition, I have also added this new mixin to our `AsyncMock` class to make sure that its `__call__` method returns a proper mock object after it has been awaited. This makes sure that subsequent attribute access on the returned object is mocked as expected.
Diffstat (limited to 'tests/test_helpers.py')
-rw-r--r--tests/test_helpers.py18
1 files changed, 15 insertions, 3 deletions
diff --git a/tests/test_helpers.py b/tests/test_helpers.py
index f08239981..62007ff4e 100644
--- a/tests/test_helpers.py
+++ b/tests/test_helpers.py
@@ -221,10 +221,10 @@ class DiscordMocksTests(unittest.TestCase):
@unittest.mock.patch(f'{__name__}.DiscordMocksTests.subTest')
def test_the_custom_mock_methods_test(self, subtest_mock):
"""The custom method test should raise AssertionError for invalid methods."""
- class FakeMockBot(helpers.AttributeMock, unittest.mock.MagicMock):
+ class FakeMockBot(helpers.GetChildMockMixin, unittest.mock.MagicMock):
"""Fake MockBot class with invalid attribute/method `release_the_walrus`."""
- attribute_mocktype = unittest.mock.MagicMock
+ child_mock_type = unittest.mock.MagicMock
def __init__(self, **kwargs):
super().__init__(spec=helpers.bot_instance, **kwargs)
@@ -331,6 +331,18 @@ class MockObjectTests(unittest.TestCase):
self.assertFalse(instance_one != instance_two)
self.assertTrue(instance_one != instance_three)
+ def test_get_child_mock_mixin_accepts_mock_seal(self):
+ """The `GetChildMockMixin` should support `unittest.mock.seal`."""
+ class MyMock(helpers.GetChildMockMixin, unittest.mock.MagicMock):
+
+ child_mock_type = unittest.mock.MagicMock
+ pass
+
+ mock = MyMock()
+ unittest.mock.seal(mock)
+ with self.assertRaises(AttributeError, msg="MyMock.shirayuki"):
+ mock.shirayuki = "hello!"
+
def test_spec_propagation_of_mock_subclasses(self):
"""Test if the `spec` does not propagate to attributes of the mock object."""
test_values = (
@@ -346,7 +358,7 @@ class MockObjectTests(unittest.TestCase):
mock = mock_type()
self.assertTrue(isinstance(mock, mock_type))
attribute = getattr(mock, valid_attribute)
- self.assertTrue(isinstance(attribute, mock_type.attribute_mocktype))
+ self.assertTrue(isinstance(attribute, mock_type.child_mock_type))
def test_async_mock_provides_coroutine_for_dunder_call(self):
"""Test if AsyncMock objects have a coroutine for their __call__ method."""