You definitely can't use __init__ for this. __init__ is used to
initialize a constructed instance. __new__ constructs the instance.
I believe that my example will only work for new style classes that
have a zero-arg __init__ method. It's obviously not working for you.
:(
Is JID a native class, like cStringIO, or just an oldstyle class
(doesn't inherit from 'object'?)? I'm not familiar with the library.
I should take a step back and ask:
Are you using dependency injection?
If not, have you considered it?
If so, all you'd need to do is create a mock object of
xmpp.protocol.JID, and pass it in to the code-under-test. It might be
too late to rearchitect your code, but it might not. While tedious at
first, it does make things a lot nicer down the road, for larger
projects.
I don't know if you've tried this, but it might work (I really hope so!)
import mox
import xmpp
m = mox.Mox()
# Do this before you replace JID
fake_jid = m.CreateMock(xmpp.protocol.JID)
m.StubOutWithMock(xmpp.protocol, "JID", use_mock_anything=True)
xmpp.protocol.JID('test').AndReturn(fake_jid)
...
# The rest of your code
--
Steve Middlekauff
smid...@gmail.com