test_telegram.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:eddie 作者: greenkey 项目源码 文件源码
def test_telegram_command(mocker):
    """ Test that the endpoint class correctly registers the commands callback
        and that the Telegram bot uses them to reply to messages.
    """

    mocker.patch('eddie.endpoints.telegram.Updater')
    mock_commandhandler = mocker.patch(
        'eddie.endpoints.telegram.CommandHandler')
    reply_text_m = mocker.patch('telegram.Message.reply_text')

    class MyBot(Bot):
        "Reversing bot"

        def default_response(self, in_message):
            return in_message[::-1]

        @command
        def start(self):
            "start command"
            return 'start command has been called'

        @command
        def other(self):
            "other command"
            return 'other command has been called'

    bot = MyBot()
    endpoint = TelegramEndpoint(
        token='123:ABC'
    )
    bot.add_endpoint(endpoint)
    bot.run()

    commands_added = [args for args,
                      kwargs in mock_commandhandler.call_args_list]
    commands_added = dict((name, handler) for name, handler in commands_added)

    assert 'start' in commands_added
    assert 'other' in commands_added

    commands_added['start'](bot, create_telegram_update('/start'))
    reply_text_m.assert_called_with(bot.start())

    commands_added['other'](bot, create_telegram_update('/other'))
    reply_text_m.assert_called_with(bot.other())

    bot.stop()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号