I am writing some unit tests and want to use Mock.
Given the following code:
qs = mock.Mock()
qs.filter.return_value = qs
item = mock.Mock()
item.do_work.return_value = "Some text"
qs.iter.return_value = iter([item])
qs = qs.filter(name='some name')
qs = qs.filter(valid_from__lte=Timezone.now())
for obj in qs:
obj.do_work()
at startup i get
TypeError: "Mock" object is not iterable
I tried the fix
@mock.patch('__builtin__.iter')
but I just can't get it to work. I was not able to figure out what really happens when the query sets the "used" for the loop.
Help is greatly appreciated!
[edited with added sample code, after proposing the first solution]
source
share