Created
April 14, 2015 13:58
-
-
Save tomoh1r/dd871a9e2154dbfedfdf to your computer and use it in GitHub Desktop.
with 文を mock する
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
# -*- coding: utf-8 -*- | |
import mock | |
import contextlib | |
@contextlib.contextmanager | |
def hoge(): | |
yield u'ハリルホジッチ' | |
@contextlib.contextmanager | |
def hoge_patched(): | |
yield u'パッチ着たハリルホジッチ' | |
if __name__ == '__main__': | |
with hoge() as x: | |
print(x) | |
# => ハリルホジッチ | |
with hoge_patched() as x: | |
print(x) | |
# => パッチ着たハリルホジッチ | |
with mock.patch('__main__.hoge', | |
side_effect=hoge_patched): | |
with hoge() as x: | |
print(x) | |
# => パッチ着たハリルホジッチ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment