[英]Python unittest case expected not matching with the actual
I am trying to mock the secrets manager client
.我正在尝试模拟secrets manager client
。 Earlier the variables weren't in the class so I was able to mock the client directly using a patch like below:早些时候变量不在类中,所以我可以使用如下补丁直接模拟客户端:
@patch('my_repo.rc.client')
and now since I am using an instance method, I need to mock the instance method.现在因为我使用的是实例方法,所以我需要模拟实例方法。
rc.py文件
import boto3
import json
from services.provisioner_logger import get_provisioner_logger
from services.exceptions import UnableToRetrieveDetails
class MyRepo(object):
def __init__(self, region):
self.client = self.__get_client(region)
def id_lookup(self, category):
logger = get_provisioner_logger()
try:
response = self.client.get_secret_value(SecretId=category)
result = json.loads(response['SecretString'])
logger.info("Got value for secret %s.", category)
return result
except Exception as e:
logger.error("unable to retrieve secret details due to ", str(e))
raise Exception("unable to retrieve secret details due to ", str(e))
def __get_client(self, region):
return boto3.session.Session().client(
service_name='secretsmanager',
region_name=region
)
test_secrt.py测试密码.py
from unittest import TestCase
from unittest.mock import patch, MagicMock
from my_repo.rc import MyRepo
import my_repo
class TestSecretManagerMethod(TestCase):
def test_get_secret_value(self):
with patch.object(my_repo.rc.MyRepo, "id_lookup") as fake_bar_mock:
fake_bar_mock.get_secret_value.return_value = {
"SecretString": '{"secret": "gotsomecreds"}',
}
actual = MyRepo("eu-west-1").id_lookup("any-name")
self.assertEqual(actual, {"secret": "gotsomecreds"})
Now, I tried a SO post to implement the same but the end result isn't matching.现在,我尝试了一个SO post来实现相同但最终结果不匹配。 It gives results like below:它给出如下结果:
self.assertEqual(actual, {"secret": "gotsomecreds"})
AssertionError: <MagicMock name='id_lookup()' id='4589498032'> != {'secret': 'gotsomecreds'}
I think I am close but unable to find out what exactly am I missing here.我想我很接近但无法找出我到底错过了什么。
OK, we want a Mock, we don't need a magic mock.好的,我们需要一个 Mock,我们不需要魔术模拟。 In fact, we want 3.事实上,我们想要 3。
First, the session一、会话
mock_session_object = Mock()
Then the client,然后客户端,
mock_client = Mock()
This mock client will return you response:这个模拟客户端会给你回复:
mock_client.get_secret_value.return_value = {
"SecretString": '{"secret": "gotsomecreds"}',
}
The session client will return this:会话客户端将返回:
mock_session_object.client.return_value = mock_client
OK.好的。 That was a lot, but we have clients inside sessions.那是很多,但我们在会议中有客户。 Pulling it togther, we have把它拉在一起,我们有
from unittest import TestCase
from unittest.mock import patch, Mock
from credentials_repo.retrieve_credentials import CredentialsRepository
import credentials_repo
class TestSecretManagerMethod(TestCase):
@patch("boto3.session.Session")
def test_get_secret_value(self, mock_session_class):
mock_session_object = Mock()
mock_client = Mock()
mock_client.get_secret_value.return_value = {
"SecretString": '{"secret": "gotsomecreds"}',
}
mock_session_object.client.return_value = mock_client
mock_session_class.return_value = mock_session_object
actual = CredentialsRepository("eu-west-1").find_by_id("db-creds")
self.assertEqual(actual, {"secret": "gotsomecreds"})
(The @path
at the top is the same as a with
inside, right?) (顶部的@path
with
内部的 @path 相同,对吧?)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.