Writing tests has become so tedious these days that I wondered if mox could easily stub.
First of all, the class.
mox_generator.py
import mox
class MoxGenerator(object):
def __init__(self):
self.mox = mox.Mox()
I'm still starting to think of it, so I wrote one for the time being. Anything is fine, stub that returns True. Without even considering the arguments. This is terrible (sweat).
python
def true_stub(self, stub_class, stub_method, num_args):
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(True)
When calling, it looks like this. Here, MyClass.hoge (x, x, x) should return True. The last number is the number of arguments to pass to MyClass.hoge.
python
moxgen= MoxGenerator()
moxgen.true_stub(MyClass, 'hoge', 3)
Of course, Replay is also troublesome, so the name is strange, but put it in the generator!
python
import contextlib
@contextlib.contextmanager
def mox_replay(self):
self.mox.ReplayAll()
yield
self.mox.VerifyAll()
Test execution side.
example_test.py
with moxgen.mox_replay():
MyClass.method_which_should_call_hoge()
I also uploaded it to github. https://github.com/norobust/mox_generator Why isn't it committed with my real name? How do you change it to a username?
Recommended Posts