aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2020-05-10 14:38:12 -0700
committerGravatar MarkKoz <[email protected]>2020-05-11 12:03:10 -0700
commit0bfd003dbfc5919220129f984dc043421e535f8c (patch)
treed8184932ec2afd69155597fb6c8352552a48d6cd
parentTest on_message_edit of token remover uses on_message (diff)
Add a test helper function to patch multiple attributes with autospecs
This helper reduces redundancy/boilerplate by setting default values. It also has the consequence of shortening the length of the invocation, which makes it faster to use and easier to read.
-rw-r--r--tests/helpers.py9
1 files changed, 9 insertions, 0 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
index 2b79a6c2a..d444cc49d 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -23,6 +23,15 @@ for logger in logging.Logger.manager.loggerDict.values():
logger.setLevel(logging.CRITICAL)
+def autospec(target, *attributes: str, **kwargs) -> unittest.mock._patch:
+ """Patch multiple `attributes` of a `target` with autospecced mocks and `spec_set` as True."""
+ # Caller's kwargs should take priority and overwrite the defaults.
+ kwargs = {'spec_set': True, 'autospec': True, **kwargs}
+ attributes = {attribute: unittest.mock.DEFAULT for attribute in attributes}
+
+ return unittest.mock.patch.multiple(target, **attributes, **kwargs)
+
+
class HashableMixin(discord.mixins.EqualityComparable):
"""
Mixin that provides similar hashing and equality functionality as discord.py's `Hashable` mixin.