[Django]-How to mock API request used inside function in a Django test?

3👍

Like you, I am also new to using mock. I believe it’s intended to work like this:

    @patch("requests.get")
    def test_get_future_assignments_with_multi_assignments(self, mock_requests_get):
        """
        Test for getting future assignments for a user with mocked API
        """
        mock_requests_get.return_value = json.dumps(self.futureassignments)
        assignments = get_future_assignments(18888)
        self.assertEqual(len(assignments), 2)

Please correct me if I’m wrong!

Leave a comment