Rudziankoŭ
Rudziankoŭ

Reputation: 11251

Mock Tornado class in Python

I would like to cover on_text method with unit test. I feel like checking on_text with non-unicode message to see that send_message was called.

class MyTornadoClass(object):
    @gen.coroutine
    def on_text(self, message):
        """
        User message controller
        """
        id_ = message.message_id
        chat = message.chat
        text = message.text.strip().replace(" ", "-").replace("[()&?]", "")
        if not self.is_ascii(text):
            yield self.send_message(chat.id_, "Sorry, I didn't find anything according to you request. Try again!",
                                    reply_to_message_id=id_)
        else:
            yield self.perform_search(text, id_, chat)

My mocking test is:

def test_app(self):
    message = types.Message({'message_id': '1', 'text': 'Ў',
                             'chat': {"id": 227071993, "first_name": "Sergei", "last_name": "Rudenkov",
                                      "type": "private"}, 'from': 343})

    zombie = bot_telegram.starter.MyTornadoClass('API_TOKEN', 'SO_WS_URL')
    zombie.on_text(message)
    with mock.patch.object(bot_telegram.starter.MyTornadoClass, 'send_message') as mock_zombie:
        mock_zombie.assert_called_with(227071993,
                                       """Sorry, I didn't find anything according to you request.
                                       Try again!""",
                                       1)

I am receiving exception:

 Traceback (most recent call last):
  File "/home/sergei-rudenkov/PycharmProjects/python_tasks/bot_telegram/unit_tests/telezombie_api/starter_test.py", line 21, in test_app
    1)
  File "/usr/local/lib/python3.5/dist-packages/mock/mock.py", line 925, in assert_called_with
    raise AssertionError('Expected call: %s\nNot called' % (expected,))
AssertionError: Expected call: send_message(227071993, "Sorry, I didn't find anything according to you request.\n                                           Try again!", 1)
Not called

----------------------------------------------------------------------
Ran 1 test in 0.006s

FAILED (failures=1)

I don't know what I am doing wrong. Please, point me out my mistake.

Upvotes: 0

Views: 316

Answers (1)

Eugene Yarmash
Eugene Yarmash

Reputation: 149776

When you use patch.object() as a context manager, the patching applies to the indented block after the with statement. If you move the call to .on_text() inside the block, you'll see a different AssertionError:

with mock.patch.object(bot_telegram.starter.MyTornadoClass, 'send_message') as mock_zombie:        
    zombie.on_text(message)
    mock_zombie.assert_called_with(args)

AssertionError: Expected call: send_message(227071993, "Sorry, I didn't find anything according to you request.\n                                           Try again!", 1)
Actual call: send_message(227071993, "Sorry, I didn't find anything according to you request. Try again!", reply_to_message_id='1')

Upvotes: 1

Related Questions