python-3.x - Python从方法调用返回一个模拟对象
问题描述
我有一个制作 dynamoDB 客户端对象的模块。该模块是外部的,不属于我的代码。我正在尝试对 enclosureClass 进行单元测试
class enclosingClass:
def enclosing_function():
ddb_client = get_ddb_client(role)
ddb_client.query()
# Module in my code which returns dynamoDB client object
from x.y.common import get_aws_client
def get_ddb_client(role):
return get_aws_client('dynamodb', assume_role_arn=role)
在我的代码中,我正在获取 aws 客户端对象并对其调用查询。我想嘲笑这两件事
- get_aws_client 的返回值应该是一个模拟对象
- 调用查询 dynamoDB 应该发生在模拟对象上。
但是当我运行单元测试时,我得到了实际的 dynamoDB 对象并在 dynamoDB 对象上调用查询返回“预期的字节或字节数组,但得到了”MagicMock
我不明白该怎么做。对 python 非常陌生,不知道如何返回一个模拟的 dynamoDb 客户端对象
@mock.patch("x.y.common.get_aws_client")
@mock.patch("boto3.client")
def test_hello_world_task(get_aws_client_mock, mock_client):
get_aws_client_mock.return_value= mock_client
mock_client.query.return_value = None
enclosing_class.enclosing_function() # the method call to enclosing function
解决方案
您的问题:dynamoDB 客户端正在enclosing_function
.
解决方案:在被测函数之外创建对象(被模拟的对象)并将其作为参数传递[也称为依赖注入]。
class enclosingClass:
def enclosing_function(get_ddb_client):
ddb_client = get_ddb_client # could avoid this line by directly taking the parameter as ddb_client
ddb_client.query()
@mock.patch("x.y.common.get_aws_client")
@mock.patch("boto3.client")
def test_hello_world_task(get_aws_client_mock, mock_client):
get_aws_client_mock.return_value= mock_client
mock_client.query.return_value = None
enclosing_class.enclosing_function(get_aws_client_mock) # the method call to enclosing function
希望这个答案。谢谢。
推荐阅读
- vue.js - 将多个图标附加到 Vuetify.js 中的输入
- algorithm - 寻找唯一可解码的代码
- javascript - 为什么在 React 单元测试中模拟事件时这是未定义的,使用 sinon.spy 模拟函数和酶.shallow 渲染 React 组件?
- spring-mvc - Spring Boot REST API 在请求映射中使用“服务”时出现“无效的内容类型”错误
- javascript - 将参数传递给 Vuex 中的 getter
- r - ggplot2 - 将所有数据标签添加到带有构面的堆叠条形图的一个部分
- time-complexity - 贪婪硬币改变时间复杂度
- javascript - 以 Javascript 形式 Java 创建字节数组
- javascript - Base64 编码图像的字符串太长,无法复制/粘贴到 MongoDB。我可以压缩它吗?
- angular - 具有角度复选框更改的 ag-grid 未在模型中更新