Skip to content

Instantly share code, notes, and snippets.

@evansde77
Created May 20, 2014 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evansde77/ce4023da802c35653335 to your computer and use it in GitHub Desktop.
Save evansde77/ce4023da802c35653335 to your computer and use it in GitHub Desktop.
Mocking example
import unittest
import mock
from wilson.views.bluemix import BluemixSingleUser
class TestBluemixUser(unittest.TestCase):
def test_get_user(self):
with mock.patch('wilson.views.bluemix.build_dao') as mock_build_dao:
with mock.patch('wilson.views.bluemix.jsonify') as mock_jsonify:
mock_dao = mock.Mock()
mock_dao.exists = mock.Mock()
mock_dao.exists.return_value = True
mock_dao.read = mock.Mock()
mock_build_dao.return_value = mock_dao
mock_dao.to_json = mock.Mock()
mock_dao.to_json.return_value = {'username': 'batman'}
mock_jsonify.return_value = "ResponseGoesHere"
bmsu = BluemixSingleUser()
result = bmsu.get(username='ringo')
self.failUnless(mock_build_dao.called)
self.failUnless(mock_dao.exists.called)
self.failUnless(mock_dao.read.called)
self.failUnless(mock_dao.to_json.called)
self.assertEqual(result, "ResponseGoesHere")
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment