【发布时间】:2016-02-23 05:13:28
【问题描述】:
假设我们有以下结构:
class A():
class __A():
def __to_be_mocked(self):
#something here
def __init__(self):
with A.lock:
if not A.instance:
A.instance = A.__A()
def __getattr__(self,name):
return getattr(self.instance,name)
现在我们要模拟函数__to_be_mocked。我们如何模拟它,因为mock.patch.object接受的目标是package.module.ClassName。我已经尝试了所有方法,例如
target = A.__A
target = A.___A
还有更多。
编辑:
我用
解决了target=A._A__A and attribute as '_A__to_be_mocked`
现在的问题是__to_be_mocked 在__A 里面,所以不应该是___A__to_be_mocked。
是因为A中的setattribute还是A中的__init__?
【问题讨论】:
标签: python python-2.7 unit-testing mocking patch