@V-ampre

Почему не срабатывает mock.side_effect?

# 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:

<class 'unittest.mock.MagicMock'>

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

Чтото делаю не так, но не могу понять что...
  • Вопрос задан
  • 76 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы