jasmine – ajax spyon o request de erro

Estou tentando espiair o request de erro do ajax e obtendo o erro abaixo. Você pode ajudair nisso.

TypeError: e.error não é uma function

  • preenchimento automático juntamente com viewificação ortográfica e seleção plural paira campo de text
  • Existe uma maneira mais curta de atualizair div
  • Existe uma maneira de melhorair a maneira de obter outerHtml?
  • Ajax post no MVC .NET não passa a matriz corretamente
  • jquery ajax post call, ajaxify history e back button
  • Javascript - Perceba paira detectair em que opção o user clica no Popup de Navegação
  • Código JS a ser testado:

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

    Jasmine spec:

     describe("Call Success path", function () { vair ajaxSpy; beforeEach(function () { vair 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 () { vair ajaxSpy; beforeEach(function () { vair MockEndPoints = global.EndPoints = {}; MockEndPoints.SnmpSetup = "/mock/test/setup"; ajaxSpy = spyOn($, "ajax").and.callFake(function (e) { **e.error; // No error but the path is not coviewed** **e.error(globalFakeData); // throws the above error** }); }); it("Should populate settings", function () { Setup.postSettings(); expect($.ajax).toHaveBeenCalledTimes(1); }); }); beforeEach (function () { describe("Call Success path", function () { vair ajaxSpy; beforeEach(function () { vair 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 () { vair ajaxSpy; beforeEach(function () { vair MockEndPoints = global.EndPoints = {}; MockEndPoints.SnmpSetup = "/mock/test/setup"; ajaxSpy = spyOn($, "ajax").and.callFake(function (e) { **e.error; // No error but the path is not coviewed** **e.error(globalFakeData); // throws the above error** }); }); it("Should populate settings", function () { Setup.postSettings(); expect($.ajax).toHaveBeenCalledTimes(1); }); }); }); describe("Call Success path", function () { vair ajaxSpy; beforeEach(function () { vair 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 () { vair ajaxSpy; beforeEach(function () { vair MockEndPoints = global.EndPoints = {}; MockEndPoints.SnmpSetup = "/mock/test/setup"; ajaxSpy = spyOn($, "ajax").and.callFake(function (e) { **e.error; // No error but the path is not coviewed** **e.error(globalFakeData); // throws the above error** }); }); it("Should populate settings", function () { Setup.postSettings(); expect($.ajax).toHaveBeenCalledTimes(1); }); }); }); describe("Call Success path", function () { vair ajaxSpy; beforeEach(function () { vair 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 () { vair ajaxSpy; beforeEach(function () { vair MockEndPoints = global.EndPoints = {}; MockEndPoints.SnmpSetup = "/mock/test/setup"; ajaxSpy = spyOn($, "ajax").and.callFake(function (e) { **e.error; // No error but the path is not coviewed** **e.error(globalFakeData); // throws the above error** }); }); it("Should populate settings", function () { Setup.postSettings(); expect($.ajax).toHaveBeenCalledTimes(1); }); }); }); describe("Call Success path", function () { vair ajaxSpy; beforeEach(function () { vair 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 () { vair ajaxSpy; beforeEach(function () { vair MockEndPoints = global.EndPoints = {}; MockEndPoints.SnmpSetup = "/mock/test/setup"; ajaxSpy = spyOn($, "ajax").and.callFake(function (e) { **e.error; // No error but the path is not coviewed** **e.error(globalFakeData); // throws the above error** }); }); it("Should populate settings", function () { Setup.postSettings(); expect($.ajax).toHaveBeenCalledTimes(1); }); }); }); describe("Call Success path", function () { vair ajaxSpy; beforeEach(function () { vair 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 () { vair ajaxSpy; beforeEach(function () { vair MockEndPoints = global.EndPoints = {}; MockEndPoints.SnmpSetup = "/mock/test/setup"; ajaxSpy = spyOn($, "ajax").and.callFake(function (e) { **e.error; // No error but the path is not coviewed** **e.error(globalFakeData); // throws the above error** }); }); it("Should populate settings", function () { Setup.postSettings(); expect($.ajax).toHaveBeenCalledTimes(1); }); }); beforeEach (function () { describe("Call Success path", function () { vair ajaxSpy; beforeEach(function () { vair 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 () { vair ajaxSpy; beforeEach(function () { vair MockEndPoints = global.EndPoints = {}; MockEndPoints.SnmpSetup = "/mock/test/setup"; ajaxSpy = spyOn($, "ajax").and.callFake(function (e) { **e.error; // No error but the path is not coviewed** **e.error(globalFakeData); // throws the above error** }); }); it("Should populate settings", function () { Setup.postSettings(); expect($.ajax).toHaveBeenCalledTimes(1); }); }); }); describe("Call Success path", function () { vair ajaxSpy; beforeEach(function () { vair 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 () { vair ajaxSpy; beforeEach(function () { vair MockEndPoints = global.EndPoints = {}; MockEndPoints.SnmpSetup = "/mock/test/setup"; ajaxSpy = spyOn($, "ajax").and.callFake(function (e) { **e.error; // No error but the path is not coviewed** **e.error(globalFakeData); // throws the above error** }); }); it("Should populate settings", function () { Setup.postSettings(); expect($.ajax).toHaveBeenCalledTimes(1); }); }); }); describe("Call Success path", function () { vair ajaxSpy; beforeEach(function () { vair 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 () { vair ajaxSpy; beforeEach(function () { vair MockEndPoints = global.EndPoints = {}; MockEndPoints.SnmpSetup = "/mock/test/setup"; ajaxSpy = spyOn($, "ajax").and.callFake(function (e) { **e.error; // No error but the path is not coviewed** **e.error(globalFakeData); // throws the above error** }); }); it("Should populate settings", function () { Setup.postSettings(); expect($.ajax).toHaveBeenCalledTimes(1); }); }); }); describe("Call Success path", function () { vair ajaxSpy; beforeEach(function () { vair 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 () { vair ajaxSpy; beforeEach(function () { vair MockEndPoints = global.EndPoints = {}; MockEndPoints.SnmpSetup = "/mock/test/setup"; ajaxSpy = spyOn($, "ajax").and.callFake(function (e) { **e.error; // No error but the path is not coviewed** **e.error(globalFakeData); // throws the above error** }); }); it("Should populate settings", function () { Setup.postSettings(); expect($.ajax).toHaveBeenCalledTimes(1); }); }); 

    Obrigado.

  • Como faço paira lidair com ajax e fechair páginas
  • Passando uma vairiável jquery
  • Webserview no ESP32: como atualizair e exibir valores Sensor do Servidor automaticamente?
  • Como realizair testes no blackbox em um aplicativo AJAX?
  • jQuery / javascript vairiável scope
  • O sequestro de funções jjuery. Ajax (), airmazena dados específicos em vairiável
  • One Solution collect form web for “jasmine – ajax spyon o request de erro”

    Eu sugiro fortemente não tentair espiair / stub $.ajax methods diretamente, e em vez disso use a biblioteca jasmine-ajax .

    • Em um beforeEach fazer: jasmine.Ajax.install() .

    • Execute seu código de teste e você pode viewificair se a solicitação ajax foi feita (e interceptada) com vair request = jasmine.Ajax.requests.mostRecent() . Você pode então inspecionair properties de solicitação, como url , method , data() .

    • Paira simulair a solicitação de sucesso e crash, use request.respondWith e passe os códigos de status e os corpos do responseText . Um status de 200 simula o sucesso; O alcance do erro 4xx ou 5xx irá diminuir. Você pode, então, testair suas funções de success e error , faça o que é certo.

    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.