python - 使用 mox 模拟一个名为 by__init__ 的方法

标签 python mox

我想在类中删除由 init 方法调用的单个方法。

class MyClass(object):
  def __init__(self): 
    # Some initializer code here
    ...
    self.method_with_side_effects()

  def method_with_side_effects(self):
    ... # Load files, etc.

根据 Mox 文档,您可以通过实例化对象然后使用 StubOutWithMock 方法来模拟方法。但在这种情况下,我不能这样做:

import mox
m = mox.Mox()
myobj = MyClass()
m.StubOutWithMock(myobj, "method_with_side_effects") # Too late!

还有其他方法可以消除该方法吗?

最佳答案

您可以直接子类化MyClass并重写method_with_side_effects吗?

关于python - 使用 mox 模拟一个名为 by__init__ 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6442018/

相关文章:

python - 模拟还是 stub ?

Python SQLAlchemy - 模拟模型属性的 "desc"方法

python - 使用 Mox 和 Python 测试模拟对象的调用顺序

python - 属性错误 : __exit__ when I try to mock out build in functions

python - '020-06-08 17 :11:02+00:00' I want to extract the date from this time format in Python using datetime

python - 无法读取上传到谷歌云存储桶的 csv 文件

python - 为什么 Jupyter Notebook 在制作更新图时会创建重复图

python - 如何在Python中按比例混合颜色?

python - sqrtm() 函数返回 nan

python - 运行 python mox 测试时从未调用的预期方法