Issue
I'm using Django version 4.2.6.
I have a file, signals.py
, that includes a pre-save handler for my entity:
def do_stuff(sender, instance, **kwargs):
... stuff...
pre_save.connect(do_stuff, sender=MyEntity)
... so that when an instance of MyEntity
is either created or updated, the do_stuff()
function is executed.
How do I mock do_stuff()
in unit tests? I have a number of tests that create and update MyEntity
instances, so do_stuff()
is being called during the execution of the unit test. My implementation of do_stuff()
is making a call to an external source amongst other functionality, so I want to mock it when the unit test runs.
I have tried declaring the following a part of my unit test:
@mock.patch("application.package.signals.do_stuff", autospec=True,)
... but when I execute the test, I can see that do_stuff()
is still being executed.
Solution
As per bbayles' suggestion above, a solution was implemented where the code to call do_stuff()
was only executed when it was deployed into an environment, rather than run locally or via unit tests.
I used os.environ
to identify whether this was the case or not.
Answered By - GarlicBread
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.