I'm trying to use Mock, to simulate a function in python. Here is my code:
resp, content = request(...)
The request() function needs to return two values. Here's what I tried:
with patch("syncdatetime.py") as sync_mock:
sync_mock.request.return_value = [obj, '']
But when I run the test, I get the error "Mock object is not iterable." The request function returns an object of type Mock instead of a list. How can I patch the request function so that it returns 开发者_C百科a list?
I suspect that your problem is that you are not using the instance of mock that you think you are. By default an instance of Mock returns a Mock when called.
>>> m = mock.Mock()
>>> type(m())
<class 'mock.mock.Mock'>
It looks like your call to request
is returning a Mock because the return_value
has not been initialized which means that resp, content = request()
is trying to unpack a Mock object.
>>> m = mock.Mock()
>>> (a, b) = m()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'Mock' object is not iterable
You shouldn't need to use side_effect
to return a list. Assigning return_value
should work.
>>> m = mock.Mock()
>>> m.return_value = ['a', 'b']
>>> (a, b) = m()
>>> a
'a'
>>> b
'b'
Note of disclosure, I'm new to mock so I'm not an expert, however, I have just suffered the same problem and found that setting the side_effect
attribute to a function that returns an array fixes things
From your example code, change:
with patch("syncdatetime.py") as sync_mock:
sync_mock.request.return_value = [obj, '']
to
with patch("syncdatetime.py") as sync_mock:
sync_mock.request.side_effect = function_returning_list
and define
def function_returning_list(arg_list_of_choice):
#anything you want to do goes here
#then
return your_list
精彩评论