Add a new @spy_for decorator for spying with fake functions.

Review Request #11026 — Created May 16, 2020 and submitted — Latest diff uploaded

Information

kgb
master

Reviewers

kgb

This introduces a new alternative to spy_on(), the @spy_for()
decorator. This is is a convenience around creating a function and then
spying using call_fake=, simplifying test code by a few lines.

@spy_for() takes a function/method to spy on and an optional owner,
and will take care of setting up a spy that passes the decorated
function as call_fake=.

For example:

def test_doomsday_device(self):
    dd = DoomsdayDevice()

    @self.spy_for(dd.kaboom)
    def _save_world(*args, **kwargs)
        print('Sprinkles and ponies!')

Which is simpler and more compact than:

def test_doomsday_device(self):
    dd = DoomsdayDevice()

    def _save_world(*args, **kwargs)
        print('Sprinkles and ponies!')

    self.spy_on(dd.kaboom, call_fake=_save_world)

The README has been updated to document this new usage, and to prefer it
in examples.

Unit tests pass on all supported versions of Python.

Commits

Files