Created
July 5, 2012 00:58
-
-
Save pfctdayelise/3050354 to your computer and use it in GitHub Desktop.
Mocking functions with decorators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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