mercredi 28 janvier 2015

How to mock.patch a plain dictionary in python?

How do I mock.patch a plain dictionary {} ?


I would like to check if the headers is set to {'Content-Type': 'application/json’}.



def get(self):
result = Spider.get_news_urls()
for k, v in result.iteritems():
response = requests.get(v)
xml = response.text()
headers = {'Content-Type': 'application/json'}
data = ''
taskqueue.Task(url='/v1/worker', headers=headers, payload=json.dumps(data)).add(queue_name='itagnewstasks')
return 'success', 200


The following unit test seems to successfully patch dict. But I have a {}, which I need to patch.



@mock.patch('__builtin__.dict')
@mock.patch('requests.get')
def test_header_is_set_to_json(self, req_get, x):
gen = Generator()
gen.get()
x.assert_called_with()


I suppose an alternative way would be to mock patch taskqueue.Task() and compare if it was called with headers= {'Content-Type': 'application/json'} as a parameter.


Aucun commentaire:

Enregistrer un commentaire