Skip to content

Instantly share code, notes, and snippets.

@aehlke
Last active August 29, 2015 14:19
Show Gist options
  • Save aehlke/2021dd34c91b09da36f1 to your computer and use it in GitHub Desktop.
Save aehlke/2021dd34c91b09da36f1 to your computer and use it in GitHub Desktop.
DI example
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