gpt4 book ai didi

python - 在模拟上设置属性不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 18:30:51 24 4
gpt4 key购买 nike

我正在尝试创建对具有属性 status_code 的 POST 方法的库(Hammock)调用的模拟。 。这是我的测试代码:

def test_send_text(self):
Hammock.POST = Mock(status_code=201)
print Hammock.POST.status_code
self.task.payload = fixtures.text_payload

self.task.send_text()
# ········
Hammock.POST.assert_any_call()

当我打印Hammock.POST.status_code时,我得到了我所期望的——201。但是,当我们进入我正在测试的代码时:

response = self.twilio_api('Messages.json').POST(data=self.payload)
print '*' * 10
print response
print response.status_code
if response.status_code == 201:
self.logger.info('Text message successfully sent.')
else:
raise NotificationDispatchError('Twilio request failed. {}. {}'.format(response.status_code,
response.content))

事情变得很奇怪。响应确实是一个 Mock 对象。但是response.status_code不是像我在测试中尝试时那样是201,而是一个对象:<Mock name='mock().status_code' id='4374061968'> 。为什么我的模拟属性在测试代码中起作用,而不是在我正在测试的代码中起作用?

最佳答案

问题是 POST().status_code 与 POST.status_code。 POST.status_code 确实会 == 201,但是 POST() 返回的对象是一个全新的模拟对象。

您要查找的内容大概是

Hammock.POST = Mock()
Hammock.POST.return_value = Mock(status_code=201)

关于python - 在模拟上设置属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22409537/

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