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

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

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.

Summary ID
Add a new @spy_for decorator for spying with fake functions.
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: ```python 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: ```python 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.
bcbd9951baf01b4e6fe97e56818da9631f4e4c5f
chipx86
david
  1. Ship It!
  2. 
      
chipx86
Review request changed
Status:
Completed
Change Summary:
Pushed to master (9b9e1da)