Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
Can't mock a third party api call

I have a view that perform a third party API call:

from .atoca_api_calls import atoca_api_call

def atoca_api_call_view(request):
  # some code
  data = atoca_api_call(**params)  # the actual API call happens in this func
  # some code  

My intention is to mock just the atoca_api_call() func and not the whole view.

class AtocaTestCase(TestCase):
    def setUp(self):
      # some code

    @patch('crm.atoca_api_calls.atoca_api_call')
    def test_atoca_call(self, mock_atoca_api_call):
      mock_atoca_api_call.return_value = MagicMock(
        status_code=200,
        response=some_json # just returns some json
      )
      url = reverse('crm:atoca-api-call')  # url related to `atoca_api_call_view`
      response = self.client.post(url, some_others_params)
      # various asserts

The test works fine but atoca_api_call() is not mocked. I'm aware of where to patch:

@patch('crm.views.atoca_api_call', autospec=True) 

Raises a ValueError:

ValueError: Failed to insert expression "<MagicMock name='mock.__getitem__().__getitem__().__getitem__()().resolve_expression()' id='140048216397424'>" on crm.Company.atoca_id. F() expressions can only be used to update, not to insert.

It's probably a simple issue I don't catch for inexperience, any help is really appreciated.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

As i see the atoca_api_call() it is mocked. The issue is the return value.

mock_atoca_api_call.return_value = MagicMock(
    status_code=200,
    response=some_json # just returns some json
)

This should ve a proper response i assume a JsonResponse or Response(data) not sure what are you returning. Try with:

mock_atoca_api_call.return_value = JsonResponse(
    some_json # just returns some json
)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!