aboutsummaryrefslogtreecommitdiffstats
path: root/tests/helpers.py
diff options
context:
space:
mode:
authorGravatar Mark <[email protected]>2020-06-12 19:42:53 -0700
committerGravatar GitHub <[email protected]>2020-06-12 19:42:53 -0700
commit9b37e3627f3c5d98fbb73a0d890bae4372ce483a (patch)
tree2a642fa732a65a4c3b0747560e4be70b87dd6a60 /tests/helpers.py
parentMerge pull request #978 from ItsDrike/unsilence-scheduler (diff)
parentMerge remote-tracking branch 'origin/master' into bug/filters/928/non-ascii-t... (diff)
Merge pull request #937 from python-discord/bug/filters/928/non-ascii-token
Make token detection more robust and completely rewrite its tests
Diffstat (limited to 'tests/helpers.py')
-rw-r--r--tests/helpers.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
index faa839370..facc4e1af 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -5,7 +5,7 @@ import itertools
import logging
import unittest.mock
from asyncio import AbstractEventLoop
-from typing import Iterable, Optional
+from typing import Callable, Iterable, Optional
import discord
from aiohttp import ClientSession
@@ -26,6 +26,24 @@ for logger in logging.Logger.manager.loggerDict.values():
logger.setLevel(logging.CRITICAL)
+def autospec(target, *attributes: str, **kwargs) -> Callable:
+ """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}
+
+ # Import the target if it's a string.
+ # This is to support both object and string targets like patch.multiple.
+ if type(target) is str:
+ target = unittest.mock._importer(target)
+
+ def decorator(func):
+ for attribute in attributes:
+ patcher = unittest.mock.patch.object(target, attribute, **kwargs)
+ func = patcher(func)
+ return func
+ return decorator
+
+
class HashableMixin(discord.mixins.EqualityComparable):
"""
Mixin that provides similar hashing and equality functionality as discord.py's `Hashable` mixin.