I want a mox generator (2)

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

I want a mox generator
I want a mox generator (2)
I want to iterate a Python generator many times
I want to print in a comprehension
I want to build a Python environment
I made a stamp generator with GAN
I want to make matplotlib a dark theme
I want to easily create a Noise Model
I want to INSERT a DataFrame into MSSQL
I want to create a window in Python
I want to make a game with Python
I don't want to take a coding test
I want to create a plug-in type implementation
I want to easily find a delicious restaurant
I want to write to a file with Python
I want to upload a Django app to heroku
I got a sqlite3.OperationalError
I want to embed a variable in a Python string
I want DQN Puniki to hit a home run
100 image processing knocks !! (021-030) I want to take a break ...
I want to give a group_id to a pandas data frame
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to transition with a button in flask
I want to climb a mountain with reinforcement learning
I want to write in Python! (2) Let's write a test
I want to find a popular package on PyPi
I want to randomly sample a file in Python
I want to easily build a model-based development environment
I want to split a character string with hiragana
I want to install a package of Php Redis
[Python] I want to make a nested list a tuple
I want to manually create a legend with matplotlib
I want a Spotify sleep timer on my PC
I want to send a business start email automatically
I want to run a quantum computer with Python
I want to bind a local variable with lambda
I want to start a jupyter environment with one command
[Python] I want to get a common set between numpy
I want to start a lot of processes from python
I want to make a click macro with pyautogui (desire)
I want to automatically generate a modern metal band name
NikuGan ~ I want to see a lot of delicious meat! !!
I want to solve Sudoku (Sudoku)
I want to make a click macro with pyautogui (outlook)
I want to use a virtual environment with jupyter notebook!
I want to install a package from requirements.txt with poetry
I want to send a message from Python to LINE Bot
I made a python text
I made a discord bot
[Visualization] I want to draw a beautiful graph with Plotly
I want to make input () a nice complement in python
I made a neural network generator that runs on FPGA
I want to create a Dockerfile for the time being.
I want to record the execution time and keep a log.
MacBookPro Setup After all I want to do a clean installation
Qiskit: I want to create a circuit that creates arbitrary states! !!
I want to do a full text search with elasticsearch + python
I want to create a system to prevent forgetting to tighten the key 1
Scraping and tabelog ~ I want to find a good restaurant! ~ (Work)
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I want to create a pipfile and reflect it in docker