aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2020-05-10 15:41:14 -0700
committerGravatar MarkKoz <[email protected]>2020-05-11 12:03:11 -0700
commit593e09299c6e4115d41bfd5b074785a5e304a8d0 (patch)
tree595c54fa50fd47edc6a130b6e8cc5dc2ec4151bd
parentTest token remover skips messages without tokens (diff)
Allow using arbitrary parameter names with the autospec decorator
This gives the caller more flexibility. Sometimes attribute names are too long or they don't follow a naming scheme accepted by the linter.
-rw-r--r--tests/helpers.py17
1 files changed, 15 insertions, 2 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
index d444cc49d..1ab8b455f 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -24,12 +24,25 @@ for logger in logging.Logger.manager.loggerDict.values():
def autospec(target, *attributes: str, **kwargs) -> unittest.mock._patch:
- """Patch multiple `attributes` of a `target` with autospecced mocks and `spec_set` as True."""
+ """
+ Patch multiple `attributes` of a `target` with autospecced mocks and `spec_set` as True.
+
+ To allow for arbitrary parameter names to be used by the decorated function, the patchers have
+ no attribute names associated with them. As a consequence, it will not be possible to retrieve
+ mocks by their attribute names when using this as a context manager,
+ """
# 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)
+ patcher = unittest.mock.patch.multiple(target, **attributes, **kwargs)
+
+ # Unset attribute names to allow arbitrary parameter names for the decorator function.
+ patcher.attribute_name = None
+ for additional_patcher in patcher.additional_patchers:
+ additional_patcher.attribute_name = None
+
+ return patcher
class HashableMixin(discord.mixins.EqualityComparable):