LoginSignup
0
0

More than 5 years have passed since last update.

mox generator とか欲しい (2)

Posted at

前回mox generator とか欲しいとか書いた続きです。

やっぱり戻り値が欲しくなったので、ret= を追加しました。ついでに繰り返し呼ぶとエラーになるので、それを防ぐべくinstanceをチェックしてますが、、、これはスマートじゃないなあ・・・

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)

あと真面目な(まともな)テストライターなら引数もチェックするだろってことで。

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

type の比較方法がわからず。
ま、とりあえずという形で。
次は、repeat_stub とか作りたい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0