Jasmine - ajax spyon error request

I am trying to view an ajax error request and get the following error. Can you help with this.

TypeError: e.error is not a function

JS code for testing:

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 Spectrum:

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);
            }); 
        });

Thank.

+4
source share
1 answer

I highly recommend not trying to directly track the / stub methods $.ajax, but use the jasmine-ajax library instead .

  • The beforeEachdo: jasmine.Ajax.install().

  • , , ajax ( ) var request = jasmine.Ajax.requests.mostRecent(). , url, method, data().

  • , request.respondWith status responseText. 200 ; 4xx 5xx . success error. .

+3

All Articles