I have developed the logic where I am initializing a few variables but with two versions.
Initially, my code looked something like this:
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager',
region_name="eu-west-1"
)
class MyRepo(object):
So here, I was able to mock the client directly using a patch
because it wasn't an instance variable.
After I implemented dependency injection
:
class MyRepo(object):
def __init__(self, region):
session = boto3.session.Session()
self.client = session.client(
service_name='secretsmanager',
region_name=region
)
Now, I am trying to mock the instance variable which is accessed using the self
keyword i.e client
.
@patch('credentials_repo.my_repo.client')
class TestSecretManagerMethod(TestCase):
However, when I run the test case it throws an error attached below:
raise AttributeError(
AttributeError: <module 'credentials_repo.my_repo' from '/Users/bgh/IdeaProjects/witboost.mesh.provisioning.outputport.snowflake/provisioner/.tox/py38/lib/python3.8/site-packages/credentials_repo/retrieve_credentials.py'> does not have the attribute 'client'
How can I mock the client? Is there any better way to do it?
source https://stackoverflow.com/questions/74880937/unable-to-mock-patch-in-python-unittest
Comments
Post a Comment