python mock assert_called_with

46,458

According to the python documentation https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.assert_called_with

'This method is a convenient way of asserting that calls are made in a particular way'

so it tests whether the parameters are being used in the correct way.

About the errors that you are receiving, I think the parameter you're passing is wrong. Its have to be something like this:

mock_twitter.PostUpdate.assert_called_with(message='msg')
Share:
46,458
user1050619
Author by

user1050619

Updated on July 29, 2022

Comments

  • user1050619
    user1050619 almost 2 years

    I'm trying to understand the assert_called_with within mock but the code I wrote throws some error.

    import os
    import twitter
    
    URL = "http://test.com"
    
    def tweet(api, message):
        if len(message) > 40:
            message = message.strip("?.,.,.")
    
        status = api.PostUpdate(message)
        return status
    
    def main():
        api = twitter.Api(consumer_key=''
                        ,consumer_secret='')
        msg = 'This is test message'
        tweet(api, msg)
    
    if __name__ == '__main__':
        main()
    

    unittest

    import unittest
    from mock import Mock
    import test
    
    class TweetTest(unittest.TestCase):
        def test_example(self):
            mock_twitter = Mock()        
            test.tweet(mock_twitter,'msg')        
            mock_twitter.PostUpdate.assert_called_with('message')        
    
    if __name__ == '__main__':
        unittest.main()
    

    I'm trying to understand what assert_called_with does here?