artem
artem

Reputation: 16777

How to easily find patch target in unittest?

I have the following module structure within a Django app:

subscriptions
   - api
     - views.py
     - serializers.py
     - tests
       test_some_view.py

   - models.py
   - signals.py

In models.py I have a GooglePlayOrder model, and in signals.py I have a signal:

@receiver(post_save, sender=GooglePlayOrder)
def save_google_play_order(sender, instance, created, **kwargs):
   pass

The GooglePlayOrder instance is being created in a serializer GooglePlayOrderSerializer from api/serializers.py, which is called by some GooglePlayOrderView in api/views.py.

And now I want to run a test from api/tests/test_some_view.py, where I want to mock the save_google_play_order signal.

Unfortunately, this implementation does not work, as I (AFAIK) should follow the imports for patching something:

@patch('subscriptions.signals.save_google_play_order')
def test_normal(self, mock):

So, how should I understand in this case what exactly I should use as a target of patch decorator?

Upvotes: 0

Views: 425

Answers (1)

Christian Hansen
Christian Hansen

Reputation: 101

Try where the mocked function is used. For example, if you use save_google_play_order in serializers.py:

@patch('subscriptions.api.serializers.save_google_play_order')

Upvotes: 1

Related Questions