Skip to content

Instantly share code, notes, and snippets.

@pfctdayelise
Created July 5, 2012 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfctdayelise/3050354 to your computer and use it in GitHub Desktop.
Save pfctdayelise/3050354 to your computer and use it in GitHub Desktop.
Mocking functions with decorators
class Foo(object):
def __init__(self, a, b):
self._a = a
self._b = b
@property
def a(self):
return self._a
def test_foo(monkeypatch):
myFoo = Foo('alpha', 'beta')
assert myFoo.a == 'alpha'
def replacementA(self):
return self._b
# call the decorator explicitly on the function to be mocked
propertyReplacementA = property(replacementA)
monkeypatch.setattr(Foo, 'a', propertyReplacementA)
assert myFoo.a == 'beta'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment