I have a python pyramid view and want to write a unittest.
def home(request):
state = request.params.get('redirect', None)
cookie = request.headers.get('Cookie')
user = identify_session_user(cookie , request.registry.settings)
response = HTTPFound(location=state)
response.set_cookie('USERINFO',
base64.b64encode(json.dumps(user).encode('ascii')),
domain='test.com')
return response
Simple unittest:-
def test_hello_world(self):
from tutorial import home
request = testing.DummyRequest()
response = hello_world(request)
self.assertEqual(response.status_code, 200)
How can I mock the idenfify_session_user function that is called inside my home view?
This might not be the exact answer your question, but is an alternative way to solve the problem.
Instead of mocking a view or the whole session machinery in Pyramid, you can do integration testing based on WebTest. This might be easier to set up, but tests are little bit slower to run.