Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The case for $.post().fail() is giving error in karma test runner. "TypeError: Cannot read property 'fail' of undefined".

How do i write test for $.post().fail()and done().

My angular code is here :
JavaScript
$scope.fetch_data = function (urlbase) {
        $.post(urlbase + "/GetBranchMapping", null, function (data) {
            $scope.branchInstanceList = data;
            $scope.$apply();
            $("#divLoading").hide();
        }, "json").fail(function (xhr, status, error) {
            if (xhr.status == 500) {
                $("#divLoading").hide();
                alert(error);
            }
            else if (xhr.status == "403") {
                alert("You are not authorized to view this page.");
                xhr.status = "0";
            }
        });
    };


And my test case is here :
JavaScript
it("should be able to hide div", function () {
        var fakeData = "This will be the new html";
        var fakeError="Error";
        spyOn($, "ajax").andCallFake(function(params) {
            params.success(fakeData);
        });

        //var elm = compiled('<div id="divLoading">loading...</div>')(scope);

        scope.fetch_data('https://localhost/reportingblotter');


        //elm.hide();
        expect($('#divLoading').css('display')).toBe('none');


    })


Spy for paramas.success() is working, whereas if i used params.fail() its not.

Please help me if anyone has solution.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900