V
V
Vampre2019-12-12 09:11:18
Django
Vampre, 2019-12-12 09:11:18

Why doesn't mock.side_effect work?

# bot/bot.py

class Bot:
    def check(self):
        if not self._check_proxy():
            raise ValueError('Proxy is invalid!')

# bot/management/commands/check_bot.py
from bot.bot import Bot

bot = Bot()

class Command(BaseCommand):
    def handle(self, *args, **options):
        print(type(bot.check))
        bot.check()

# tests/test_commands.py

class TestCheckCmd(TestCase):
    @patch('bot.bot.Bot.check')
    def test_check_bot_fail(self, mock_check):
        mock_check.side_effect = ValueError
        with self.assertRaises(ValueError) as exc:
            call_command('check_bot')

Print: Traceback (most recent call last): File "/home/vagrant/.local/lib/python3.8/unittest/mock.py", line 1342, in patched return func(*newargs, **newkeywargs ) File "/home/vagrant/projects/catch_bot/bot/tests/test_management.py", line 37, in test_check_bot_fail call_command('check_bot') AssertionError: ValueError not raised I'm doing something wrong, but I can't figure out what...
<class 'unittest.mock.MagicMock'>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question