'How to magic mock long chained calls?
To test the following functino, I want to assert .filter() is called once with parameter filters.
def get_instances(session: boto3.session.Session, filters):
instances = session.resource('ec2').instances.filter(Filters=filters)
return instances
I tried to write the unit test,
def test_get_instance():
mocked_session: boto3.session.Session = boto3.session.Session()
def resource(service_name) -> boto3.resources.base.ServiceResource:
if service_name == 'ec2':
return MagicMock()
raise Exception('Parameter should be ec2')
mocked_session.resource = MagicMock(side_effect=resource) # resource is set?
mocked_session.resource('ec2').instances = MagicMock()
# mocked_session.resource('ec2').instances. ??? # not finished
filters = None
Node.get_nodes(mocked_session, filters)
assert session.resource('ec2').instances.filter.call_count == 1
However, the test function got the following error
> assert session.resource('ec2').instances.filter.call_count == 1
E AttributeError: 'function' object has no attribute 'resource'
Solution 1:[1]
You can use the built-in monckeypatch fixture for that purpose.
def test_get_instance(monkeypatch):
# ... your code
filter_calls = []
monkeypatch.setattr(mocked_session.resource('ec2'), 'filter', lambda Filters: filter_calls.append(1))
assert len(filter_calls) == 1
I did not test it with the boto3 lib, however, the idea is here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
