Last active
August 29, 2015 14:19
-
-
Save aehlke/2021dd34c91b09da36f1 to your computer and use it in GitHub Desktop.
DI example
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 SakaiAPI(object): | |
def authenticate(self, token): | |
return requests.post(...).json() | |
class MockSakaiAPI(SakaiAPI): | |
def __init__(): | |
self.is_authenticated = True | |
def authenticate(self, token): | |
return {'is_authenticated': self.is_authenticated} | |
def get_sakai_grades(token, sakai_api=SakaiAPI()): | |
sakai_api.authenticate(token) | |
sakai_api.get_grades(...) | |
... | |
def my_sakai_test(): | |
sakai_api = MockSakaiAPI() | |
sakai_api.is_authenticated = False | |
token = ... | |
self.assertRaises(lambda: get_sakai_grades(token, sakai_api=sakai_api), Exception) | |
sakai_api.is_authenticated = True | |
self.assertEqual(get_sakai_grades(token, sakai_api=sakai_api)['grade_data'], ...) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment