jeudi 5 mars 2015

How to make a distinction between static methods and instance methods when mocking a class?

I came across a bug in production, even though it should have been tested by the unit tests.



class Stage2TaskView(MethodView):
def post(self):
json_data = json.loads(request.data)
news_url_string = json_data['news_url_string']
OpenCalais().generate_tags_for_news(news_url_string) // ?
return "", 201


This used to be a static:



OpenCalais.generate_tags_for_news(news_url_string)


But then I changed the method and removed the static decorator. But I forgot to change that line to



OpenCalais().generate_tags_for_news(news_url_string)


The test doesn't see it though. How can I test this in future?



@mock.patch('news.opencalais.opencalais.OpenCalais.generate_tags_for_news')
def test_url_stage2_points_to_correct_class(self, mo):
rv = self.client.post('/worker/stage-2', data=json.dumps({'news_url_string': 'x'}))
self.assertEqual(rv.status_code, 201)

Aucun commentaire:

Enregistrer un commentaire