mercredi 15 janvier 2020

How do you mock an aws service that isn't supported by moto?

I'm using moto to mock out aws services to write my test cases and supported use cases is fine:

@mock_sts
def test_check_aws_profile(self):
    session = boto3.Session(profile_name='foo')
    client = session.client('sts')
    client.get_caller_identity().get('Account')

But there are a few services like es that isn't supported at all. How do I begin to mock this?

I've tried this alternative mocking approach for testing sts (supported by moto), but I'm looking for a more moto like approach.

import boto3

def function():
    session = boto3.Session(profile_name='foobar')
    client = session.resource('sts')
    return client.get_caller_identity().get('Account')

@patch('module.boto3')
def test_function(mocked_boto):
    mocked_session = mocked_boto.Session()
    mocked_client = mocked_session.resource()
    mocked_identity = mocked_client.get_caller_identity()

    mocked_identity.get.return_value = 'foo-bar-baz'

    result = module.function()
    assert result == 'foo-bar-baz'

    # we can make sure mocks were called properly, for example
    mocked_identity.get.assert_called_once_with('Account')

Aucun commentaire:

Enregistrer un commentaire