AngularJS $http error function never called

后端 未结 5 1038
陌清茗
陌清茗 2020-12-09 03:22

I have that simple code :

$http.get(\"/api/test\")
    .success(function (data, status, headers, config) {
        console.log(data);
        return data;
           


        
5条回答
  •  孤城傲影
    2020-12-09 03:50

    We moved the post to a factory, we don't really care here about testing the $http stuff, just what it does on success or error:

    factoryModule.factory('GiftFactory', function ($http, Settings) {
        var saveUrl = Settings.endpoints.saveUrl;
    
        return {
            createGift: function (data) {
                var gift = { gift: JSON.stringify(angular.toJson(data.gift)) };
                return $http.post(saveUrl, gift);
            }
        };
    });
    

    We call it like this:

        $scope.submit = function () {
            $scope.submitting = true;
            GiftFactory.createGift($scope)
                .success(function () {
                    $window.location = Giving.endpoints.indexUrl;
                }).error(function() {
                    $scope.submitting = false;
                    alert('an error occurred');
            });
        };
    

    We test it like this:

    describe('donation controller', function () {
    var $scope, q, window, controller;
    
    beforeEach(module('giving.donation'));
    beforeEach(inject(function ($controller, $rootScope, $q) {
        $scope = $rootScope.$new();
        window = {};
        q = $q;
        giftFactory = {};
        controller = $controller('DonationController', { $scope: $scope, $window: window, GiftFactory: giftFactory });
    }));
    
    describe('submit', function () {
        var deferred;
        beforeEach(function () {
            deferred = q.defer();
    
            deferred.promise.success = function (fn) {
                deferred.promise.then(
                    function (response) {
                        fn(response.data, response.status, response.headers);
                    });
                return deferred.promise;
            };
            deferred.promise.error = function (fn) {
                deferred.promise.then(null,
                    function (response) {
                        fn(response.data, response.status, response.headers);
                    });
                return deferred.promise;
            };
        });
    
        it('should redirect on success', function () {
            //Arrange
            Giving = { endpoints: { indexUrl: "/testurl" } };
    
            giftFactory.createGift = function () {
                return deferred.promise;
            };
    
            //Act
            $scope.submit();
    
            deferred.resolve({});
            $scope.$apply();
    
            //Assert
            expect($scope.submitting).toBeTruthy();
            expect(window.location).toBe('/testurl');
        });
    
        it('should set submitting back to false on error', function () {
            //Arrange
            Giving = { endpoints: { indexUrl: "/testurl" } };
    
            giftFactory.createGift = function () {
                return deferred.promise;
            };
    
            //Act
            $scope.submit();
    
            deferred.reject({});
            $scope.$apply();
    
            //Assert
            expect($scope.submitting).toBeFalsy();
            expect(window.location).toBeUndefined();
        });
    });
    
    });
    

提交回复
热议问题