python mock patch : a method of instance is called?

Posted by JuanPablo on Stack Overflow See other posts from Stack Overflow or by JuanPablo
Published on 2014-08-25T04:18:08Z Indexed on 2014/08/25 4:19 UTC
Read the original article Hit count: 206

In python 2.7, I have this function

from slacker import Slacker


def post_message(token, channel, message):
    channel = '#{}'.format(channel)

    slack = Slacker(token)
    slack.chat.post_message(channel, message)

with mock and patch, I can check that the token is used in Slacker class

import unittest
from mock import patch

from slacker_cli import post_message


class TestMessage(unittest.TestCase):

    @patch('slacker_cli.Slacker')
    def test_post_message_use_token(self, mock_slacker):
        token = 'aaa'
        channel = 'channel_name'
        message = 'message string'

        post_message(token, channel, message)

        mock_slacker.assert_called_with(token)

how I can check the string use in post_message ?

I try with

mock_slacker.chat.post_message.assert_called_with('#channel') 

but I get

AssertionError: Expected call: post_message('#channel')
Not called

© Stack Overflow or respective owner

Related posts about unit-testing

Related posts about python-2.7