Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda