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

348
Visualizações
jasmine - ajax spyon the error request

I am trying to spy the ajax error request and getting the below error. Can you help on this.

TypeError: e.error is not a function

JS code to be tested :

function postSettings() {

        $.ajax(
            {
                type: "POST",
                url: EndPoints.Setup,
                data: frm_data,
                success: function (successData) {
                        ///// code is getting covered
                },
                error: function (errorData) {
                        ///// code is not getting covered
                }
            });
    }

Jasmine spec:

describe("Call Success path", function () {
            var ajaxSpy;
            beforeEach(function () {
                var MockEndPoints = global.EndPoints = {};
                MockEndPoints.SnmpSetup = "/mock/test/setup";
                ajaxSpy = spyOn($, "ajax").and.callFake(function (e) {
                    e.success(globalFakeData);
                });
            });

            it("Should populate settings", function () {
                Setup.postSettings();
                expect($.ajax).toHaveBeenCalledTimes(2);
            });

        });

        describe("Call Error path", function () {
            var ajaxSpy;
            beforeEach(function () {
                var MockEndPoints = global.EndPoints = {};
                MockEndPoints.SnmpSetup = "/mock/test/setup";
                ajaxSpy = spyOn($, "ajax").and.callFake(function (e) {
                    **e.error; // No error but the path is not covered**
                    **e.error(globalFakeData); // throws the above error**
                });
            });

            it("Should populate settings", function () {
                Setup.postSettings();
                expect($.ajax).toHaveBeenCalledTimes(1);
            }); 
        });

Thanks.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I'd strongly suggest not trying to spy on / stub $.ajax methods directly, and instead use the jasmine-ajax library.

  • In a beforeEach do: jasmine.Ajax.install().

  • Run your test code, and you can check the ajax request has been made (and intercepted) with var request = jasmine.Ajax.requests.mostRecent(). You can then inspect request properties such as url, method, data().

  • To simulate the request succeeding and failing, use request.respondWith and pass in status codes and responseText bodies. A 200 status will simulate success; 4xx or 5xx range will go down the error path. You can then test your success and error functions do the right thing.

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