base.Base.assignment
简单地用一个Mock
对象代替。您 提出 通过添加它描述__get__
方法。
这有点冗长,有些不必要。您可以base.Base.assignment
直接直接设置:
def test_empty(self):
Base.assignment = {}
assert len(Base().assignment.values()) == 0
当然,在使用测试并发时,这不太安全。
要使用PropertyMock
,我将使用:
with patch('base.Base.assignment', new_callable=PropertyMock) as a:
a.return_value = {'a': 1}
甚至:
with patch('base.Base.assignment', new_callable=PropertyMock,
return_value={'a': 1}):