'How to patch local function of a variable?

I tried to write a test for this but I keep getting an error that the list is not callable. How can I patch nodes.items() so that items returns a list?

# Get list of hosts via ZK
nodes = getNodes()

# Send out query to each node simultaneously
for (node_id, node) in nodes.items():
    LOGGER.debug(f"node: {node_id}, {node}"

I tried to do this:

monkeypatch.setattr("application.controllers.external_api.items", mock.MagicMock(return_value=record))

where record is:

record = {
    'id': '1',
    'update': 'test',
    'items': []
}

But I get:

TypeError: 'list' object is not callable

How can I patch this nodes.items() function?

When I do this:

monkeypatch.setattr("application.controllers.external_api.items", mock.MagicMock(return_value=record))

it states "items" is not a package in my file.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source