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

343
Views
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 answers
Answer question

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 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!