我在生产中遇到了一个bug,尽管它应该通过单元测试进行测试。
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这曾经是静态的:
OpenCalais.generate_tags_for_news(news_url_string)但后来我改变了方法,去掉了静态装饰器。但我忘了把这句话改成
OpenCalais().generate_tags_for_news(news_url_string)不过,测试看不见。我以后怎么测试这个?
@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)发布于 2015-03-06 12:57:01
自噬是你的油炸!在修补程序中使用autospec=True将检查完整的签名:
class A():
def no_static_method(self):
pass
with patch(__name__+'.A.no_static_method', autospec=True):
A.no_static_method()将引起一个例外:
Traceback (most recent call last):
File "/home/damico/PycharmProjects/mock_import/autospec.py", line 9, in <module>
A.no_static_method()
TypeError: unbound method no_static_method() must be called with A instance as first argument (got nothing instead)https://stackoverflow.com/questions/28888112
复制相似问题