我已经以两种不同的方式为'a.foo'启动了一个模拟补丁。在TestUtils1中,立即定义了类变量。在TestUtils2中,将调用设置函数。为什么根据初始化,mock_config_file的类型有所不同?运行后,PropertyMock和MagicMock有所不同。
class TestUtils1:
config_file_patcher = mock.patch('a.foo',new_callable=mock.PropertyMock) <mock.mock._patch object>
mock_config_file = config_file_patcher.start() <PropertyMock name='_config_file' id='413585304'>
class TestUtils2:
config_file_patcher = None
mock_config_file = None
@classmethod
def setup(cls):
cls.config_file_patcher = mock.patch('a.foo', new_callable=mock.PropertyMock) <mock.mock._patch object>
cls.mock_config_file = cls.config_file_patcher.start() <MagicMock name='_config_file()' id='471135344'>
TestUtils1的结果是我期望的结果。在TestUtils2中,为什么ock_mock_config_file类型为MagicMock而不是PropertyMock?