I want to patch an assignment template_tag but that question is not Django specific.
Here is what I'm trying to achieve:
@patch('the_caller_path.my_function', _my_mock)
class MyTests(...):
And the "obvious" error I'm getting:
AttributeError: <module 'the_caller_path' from '...'> does not have the attribute 'my_function'
The problem is that the_caller dynamically calls my_function.
As expected, using the path were the function is declared has no effect:
@patch('the_real_path.my_function', _my_mock)
class MyTests(...):
my_function is not patched.
How can I globally patch a function?
patch.objectis the way to handle these cases, but you should provide a minimal runnable example or I can do just some speculations. I've already filed an answer on similar topic at Django/Python unittesting: How to Force Exception of Try/Except Block but I'm not sure if I can adapt it to your case.