django - Python mock to create a fake object returns dictionary when any of it's attribute is used -


for example have method in django reuses request object:

def dowork(request):     # sessionid query param of callback payment gateway     print request.get.get('sessionid') 

when write unittest, need create fake request object, should has get attribute , should contains dictionary {'sessionid': 'blah'}

how do using mock package?

do creating mock, , setting attributes other python object.

the caveat mocks specialized attributes automatically padded mocks, makes easy. don't need explicitly create each attribute mock object.

for example,

import mock mock_request = mock.mock()  mock_request.get.get.return_value = {'sessionid': 'blah'}  x = mock_request.get.get('sessionid')  assert x == {'sessionid': 'blah'} mock_request.get.get.assert_called_once_with('sessionid') 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -