python - 当你可以替换一个对象时为什么要模拟?
问题描述
当我可以直接替换我想模拟的对象时,我试图理解为什么我需要使用模拟。
例如在这个人为的例子中
class Child:
def get(self):
return {0 : "SomeDictResult"}
class Root:
def __init__(self):
self.child = Child()
def get_info(self):
return self.child.get()
def test_get_info(root : Root):
root.child.get = lambda : {1 : "SomeOtherResult"} # This is where a mock would be
print(root.get_info())
我没有模拟,而是将 get 替换为具有自定义输出的 lambda 函数。get
如果我可以替换函数,为什么我需要模拟
解决方案
推荐阅读
- r - ObserveEvent 的 SEPTA Map R 闪亮问题
- eclipse - 使用终端时,自定义单元测试文件夹/路径在 Maven 项目中看不到自定义 src 文件夹/路径
- grpc - 基于 gRPC 方法和参数的 Envoy 路由
- qt - 使用 PyQt 连接到 SQL Server
- kubernetes - 如何在具有先前 etcd 服务的 Kubernetes 集群上部署 etcd 集群
- c# - 在 C# 中,如何在运行时创建值类型变量?
- ios - UILabel 没有持续更新
- assembly - 循环指令不停止
- java - 需要签署一个java桌面应用程序
- struct - 将值推送到 Julia 中的结构元素时出现问题