Mocker has a patching function that it says allows you to replace a class, so that all instances of this class will be patched through to your mock.
I am trying to do some testing of a function that uses pysnmp, and I am trying to mock out the calls that use this library.
In my code I do this:
from pysnmp.entity.rfc3413.oneliner import cmdgen
commandGen = cmdgen.CommandGenerator()
... code that uses commandGen
In my test I tried:
from mocker import Mocker
mocker = Mocker()
commandGenMock = mocker.patch(cmdgen.CommandGenerator)
commandGenMock.doSomething()
mocker.replay()
and I get the following:
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'
Can anyone shed any light on what I am doing wrong?
Just a tip, in your test, try to mock the instance instead of the class: