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

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


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>'):        ...