gpt4 book ai didi

python mock assert_call_with

转载 作者:行者123 更新时间:2023-12-03 20:30:54 25 4
gpt4 key购买 nike

我试图了解 assert_called_with在模拟中,但我编写的代码引发了一些错误。

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()

单元测试

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()

我想了解什么 assert_called_with在这里吗?

最佳答案

根据python文档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'



所以它测试参数是否以正确的方式使用。

关于您收到的错误,我认为您传递的参数是错误的。它必须是这样的:
mock_twitter.PostUpdate.assert_called_with(message='msg')

关于python mock assert_call_with,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47576348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com