Mocker有一个补丁函数,它说允许你替换一个类,所以这个类的所有实例都将被修补到你的模拟。
我正在尝试对使用pysnmp的函数进行一些测试,我正在尝试模拟使用此库的调用。
在我的代码中,我这样做:
from pysnmp.entity.rfc3413.oneliner import cmdgen
commandGen = cmdgen.CommandGenerator()
... code that uses commandGen
在我的测试中,我尝试过:
from mocker import Mocker
mocker = Mocker()
commandGenMock = mocker.patch(cmdgen.CommandGenerator)
commandGenMock.doSomething()
mocker.replay()
我得到以下内容:
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
mocker.replay()
File "build\bdist.win32\egg\mocker.py", line 578, in replay
event.replay()
File "build\bdist.win32\egg\mocker.py", line 1757, in replay
task.replay()
File "build\bdist.win32\egg\mocker.py", line 2197, in replay
self.is_monitoring))
File "build\bdist.win32\egg\mocker.py", line 2157, in patch_attr
setattr(obj, attr, value)
TypeError: can't set attributes of built-in/extension type 'classobj'
任何人都可以了解我做错了什么吗?
答案 0 :(得分:0)
只是提示,在测试中,尝试模拟实例而不是类:
from mocker import Mocker
mocker = Mocker()
commandGen = cmdgen.CommandGenerator()
commandGenMock = mocker.patch(commandGen)
commandGenMock.doSomething()
mocker.replay()