Add a new @spy_for decorator for spying with fake functions.
Review Request #11026 — Created May 16, 2020 and submitted
This introduces a new alternative to
spy_on()
, the@spy_for()
decorator. This is is a convenience around creating a function and then
spying usingcall_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 ascall_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 |
---|---|
bcbd9951baf01b4e6fe97e56818da9631f4e4c5f |
- Change Summary:
-
kwarga
->kwargs
in examples. - Description:
-
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, **kwarga)
~ 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, **kwarga)
~ 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. - Commits:
-
Summary ID 5b3364a72d5c6441c0fef83b3e2d6babcd0bc612 bcbd9951baf01b4e6fe97e56818da9631f4e4c5f - Diff:
-
Revision 2 (+236 -14)