1

In ViewSet in my DRF project: Let's say I have a post request in which I firstly get data from external api with requests.get, and then I serialize and return it as should be in post request. I am writing unittest with django unittest and I want to mock external api call. This is my first time with mock and I can't find such a case.

To sum up:

  • I have post function which gets data from external api if user hasn't passed something.
  • I want to mock result of requests.get but I don't know how to implement it to test for post function

I don't know If It would help you to get code snippets or my story is enough. If so, I can add it just let me know.

D Malan
  • 10,272
  • 3
  • 25
  • 50
MartynaMajch
  • 118
  • 1
  • 1
  • 10
  • 1
    Yes some snippets would help and also can you show what you have tried? – Brian Destura Jan 12 '22 at 21:56
  • Does this answer your question? [How can I mock requests and the response?](https://stackoverflow.com/questions/15753390/how-can-i-mock-requests-and-the-response) – D Malan Jan 13 '22 at 00:08

1 Answers1

0

You can use python unittest mock. Here is the official document: https://docs.python.org/3/library/unittest.mock.html

MSI Shafik
  • 139
  • 6