简体   繁体   中英

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'

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.

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