如何模拟函数以 return 响应属性?
How to mock function to return response with attributes?
我正在弄乱 unittest.mock
,遇到了一些问题。
我有一个对象 client
,方法 get_messages()
,returns response
,属性 data
和 has_more
。我想在第一次调用中将其模拟为 return 固定 data
和 has_more
,在第二次调用中将其模拟为固定 data
和 has_more
。
在第一次调用中,我想接收具有以下属性的对象 response
:
data=['msg1', 'msg2']
has_more=True
在第二次调用中,我想接收具有属性的对象 response
:
data=['msg3', 'msg4']
我一直在尝试这样做,但我有点困惑,不知道是不是这样。
@patch('Client')
def test_client_returns_correct_messages(self, MockClient):
MockWebClient.get_messages.side_effects = [
Mock(name='response',
data={'messages': received_messages,
'has_more': True}),
Mock(name='response',
data={'messages': received_messages,
'has_more': False})]
messages = client.get_messages()
根据docs
If you pass in an iterable, it is used to retrieve an iterator which must yield a value on every call. This value can either be an exception instance to be raised, or a value to be returned from the call to the mock (DEFAULT handling is identical to the function case).
下面是一个调用示例,两次调用来自同一个方法,但有两个不同的答案。
import os
from unittest.mock import patch
@patch('os.path.curdir', side_effect=[True, False])
def test_side_effect(mock_curdir):
print(os.path.curdir())
print(os.path.curdir())
>>> test_side_effect()
True
False
好的,我找到了答案...通常我的代码没问题,但我打错了:side_effects
而不是 side_effect
- 注意 s.应该是side_effect
。 Mock 接受一切,所以它没有引发错误。下次肯定会使用规范 :D 我仍然不知道这是否是正确的方法,但它有效。
这是工作代码:
@patch('Client')
def test_client_returns_correct_messages(self, MockClient):
MockWebClient.get_messages.side_effect = [
Mock(name='response',
data={'messages': received_messages,
'has_more': True}),
Mock(name='response',
data={'messages': received_messages,
'has_more': False})]
messages = client.get_messages()
我正在弄乱 unittest.mock
,遇到了一些问题。
我有一个对象 client
,方法 get_messages()
,returns response
,属性 data
和 has_more
。我想在第一次调用中将其模拟为 return 固定 data
和 has_more
,在第二次调用中将其模拟为固定 data
和 has_more
。
在第一次调用中,我想接收具有以下属性的对象 response
:
data=['msg1', 'msg2']
has_more=True
在第二次调用中,我想接收具有属性的对象 response
:
data=['msg3', 'msg4']
我一直在尝试这样做,但我有点困惑,不知道是不是这样。
@patch('Client')
def test_client_returns_correct_messages(self, MockClient):
MockWebClient.get_messages.side_effects = [
Mock(name='response',
data={'messages': received_messages,
'has_more': True}),
Mock(name='response',
data={'messages': received_messages,
'has_more': False})]
messages = client.get_messages()
根据docs
If you pass in an iterable, it is used to retrieve an iterator which must yield a value on every call. This value can either be an exception instance to be raised, or a value to be returned from the call to the mock (DEFAULT handling is identical to the function case).
下面是一个调用示例,两次调用来自同一个方法,但有两个不同的答案。
import os
from unittest.mock import patch
@patch('os.path.curdir', side_effect=[True, False])
def test_side_effect(mock_curdir):
print(os.path.curdir())
print(os.path.curdir())
>>> test_side_effect()
True
False
好的,我找到了答案...通常我的代码没问题,但我打错了:side_effects
而不是 side_effect
- 注意 s.应该是side_effect
。 Mock 接受一切,所以它没有引发错误。下次肯定会使用规范 :D 我仍然不知道这是否是正确的方法,但它有效。
这是工作代码:
@patch('Client')
def test_client_returns_correct_messages(self, MockClient):
MockWebClient.get_messages.side_effect = [
Mock(name='response',
data={'messages': received_messages,
'has_more': True}),
Mock(name='response',
data={'messages': received_messages,
'has_more': False})]
messages = client.get_messages()