pytest-dev/pytest-mock

mocker.patch does not change return value

louis030195 opened this issue · 3 comments

In
https://github.com/different-ai/embedbase/blob/601c4c227993b8c081ddf3eeaddeca66423bd1ca/embedbase/test_end_to_end.py#L334

I'm trying to mock embedbase.utils.get_user_id returning user1 but when calling the function it does return None instead, is there something I did wrong or misunderstood?

mocker.patch("embedbase.utils.get_user_id", return_value="user1")

Thanks!

Hi,

Did not take a deep look at the code, but probably is the matter of patching in the wrong place, see Where to patch.

I tried to patch on every single lines without better result :/

PS: maybe related to something with async?

If you print the get_user_id at the place where it is used (IOW, where you expect the patch to have changed it) you will notice that it points to the original function, not to a MagicMock object. This might give a clue where you should be patching.

Another thing you can try is to write assert 0 in the line you call mocker.patch and another assert 0 in the line where get_user_id is used: if the latter is hit first, then it means you are patching it too late.

PS: maybe related to something with async?

Don't think so, shouldn't really affect mocking.