简体   繁体   中英

Make unittest.mock.Mock() return list of dicts

I'm trying to mock a call of method extra_get() which usually returns a list of dicts. As far as I understand from the Mock docs , if I want to return iterable, I should set side_effect param.

client.extra_get = mock.Mock(
            **{'side_effect': [{'foo': 'bar'}]})

But then the following code calls that mocked method:

extra = client.extra_get(request, type_id)
result = {x.key: x.value for x in extra}
return result

And the dict comprehention fails because extra there is not a list, but dict {'foo': 'bar'} . What I'm doing wrong? How can I make Mock method return a list of dicts?

with mock.patch.object(client, 'extra_get', return_value=[{...}, {...}]) as mock_get:
  # fill in the rest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM