Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda