diff options
author | 2020-05-22 15:44:04 -0700 | |
---|---|---|
committer | 2020-05-22 15:44:04 -0700 | |
commit | 45e6f8dba869a367b01d99a596bd3355802d1fbe (patch) | |
tree | 94fe6ab3848ba59bd7f3ca0941625edcf4c4ad78 | |
parent | Fix assertion for `create_task` in duck pond tests (diff) |
Improve aiohttp context manager mocking in snekbox tests
I'm not sure how it even managed to work before. It was calling the
`post` coroutine (without specifying a URL) and then changing
`__aenter__`. Now, a separate mock is created for the context manager
and the `post` simply returns that mocked context manager.
-rw-r--r-- | tests/bot/cogs/test_snekbox.py | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/tests/bot/cogs/test_snekbox.py b/tests/bot/cogs/test_snekbox.py index 1dec0ccaf..84b273a7d 100644 --- a/tests/bot/cogs/test_snekbox.py +++ b/tests/bot/cogs/test_snekbox.py @@ -21,7 +21,10 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): """Post the eval code to the URLs.snekbox_eval_api endpoint.""" resp = MagicMock() resp.json = AsyncMock(return_value="return") - self.bot.http_session.post().__aenter__.return_value = resp + + context_manager = MagicMock() + context_manager.__aenter__.return_value = resp + self.bot.http_session.post.return_value = context_manager self.assertEqual(await self.cog.post_eval("import random"), "return") self.bot.http_session.post.assert_called_with( @@ -41,7 +44,10 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): key = "MarkDiamond" resp = MagicMock() resp.json = AsyncMock(return_value={"key": key}) - self.bot.http_session.post().__aenter__.return_value = resp + + context_manager = MagicMock() + context_manager.__aenter__.return_value = resp + self.bot.http_session.post.return_value = context_manager self.assertEqual( await self.cog.upload_output("My awesome output"), @@ -57,7 +63,10 @@ class SnekboxTests(unittest.IsolatedAsyncioTestCase): """Output upload gracefully fallback if the upload fail.""" resp = MagicMock() resp.json = AsyncMock(side_effect=Exception) - self.bot.http_session.post().__aenter__.return_value = resp + + context_manager = MagicMock() + context_manager.__aenter__.return_value = resp + self.bot.http_session.post.return_value = context_manager log = logging.getLogger("bot.cogs.snekbox") with self.assertLogs(logger=log, level='ERROR'): |