Python mock patch doesn't work as expected for public method

北城以北 提交于 2019-11-29 09:55:45

I believe your problem is that you're not patching in the right namespace. See where_to_patch documentation for unittest.mock.patch.

Essentially, you're patching the definition of get_feed() in mrss.feed_burner but your view handler feed() already has a reference to the original mrss.feed_burner.get_feed(). To solve this problem, you need to patch the reference in your view file.

Based on your usage of get_feed in your view function, I assume you're importing get_feed like so

view_file.py

from mrss.feed_burner import get_feed

If so, you should be patching view_file.get_feed like so:

def test_feed(self):
    with patch('view_file.get_feed', new=lambda: '<xml></xml>'):
        ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!