Last active
December 19, 2015 23:59
-
-
Save untitaker/6038880 to your computer and use it in GitHub Desktop.
Mocking modules
This file contains hidden or 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
import sublime | |
def better_os(): | |
x = sublime.get_os() | |
if x: | |
return x.upper() | |
else: | |
return None |
This file contains hidden or 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
import sys | |
from mock import patch | |
class FakeSublime(): | |
pass | |
sys.modules['sublime'] = FakeSublime | |
import sublime | |
import my_module | |
def test_stuff(): | |
with patch('sublime.get_os', new=lambda: 'windows', create=True): | |
assert my_module.better_os() == 'WINDOWS' | |
with patch('sublime.get_os', new=lambda: False, create=True): | |
assert my_module.better_os() is None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment