This is a continuation of what I wrote last time I want a mox generator.
After all I wanted a return value, so I added ret =
. By the way, if you call it repeatedly, an error will occur, so I check the instance to prevent it, but this is not smart ...
mox_generator.py
def stub(self, stub_class, stub_method, num_args, ret=None):
stubbed = getattr(stub_class, stub_method)
if not isinstance(stubbed, mox.MockAnything) and not isinstance(stubbed, mox.MockObject):
self.mox.StubOutWithMock(stub_class, stub_method)
stubbed = getattr(stub_class, stub_method)
args = tuple([mox.IgnoreArg() for i in range(num_args)])
stubbed(*args).AndReturn(ret)
Also, a serious (decent) test writer would check the arguments as well.
mox_generator.py
def stub_with_args(self, stub_class, stub_method, num_args,
*args, **kwds):
if kwds.has_key('ret'):
ret = kwds.pop('ret')
else:
ret = None
stubbed = getattr(stub_class, stub_method)
if not isinstance(stubbed, mox.MockAnything) and not isinstance(stubbed, mox.MockObject):
self.mox.StubOutWithMock(stub_class, stub_method)
stubbed = getattr(stub_class, stub_method)
stubbed = getattr(stub_class, stub_method)
stub_args = tuple(map(self.stub_arg, args))
stub_kwds = {}
for key in kwds.keys():
stub_kwds[key] = self.stub_arg(kwds[key])
stubbed(*stub_args, **stub_kwds).AndReturn(ret)
def stub_arg(self, arg):
if arg is None:
return mox.IgnoreArg()
elif type(arg) == type(MoxGenerator):
return mox.IsA(arg)
else:
return arg
I don't know how to compare type
.
Well, for the time being.
Next, I want to make something like repeat_stub
.
Recommended Posts