数据在 Header 中的单元测试 Angular http.post

Unit Test Angular http.post with Data in Header

我正在尝试为执行 http.post 到 api 的服务编写单元测试,该 api 在 header.

中传递凭据

控制器:

app.controller('LoginController', function($scope, $http, signInService) {

    $scope.LogIn = function(usrnm, pwd) {
        signInService.authUser(usrnm, pwd)
        .success(function (data, status, headers, config) {
            // Display success message
            $scope.gotToAddress = data.successUrl;
        })
        .error(function (data, status, headers, config) {
           // Display error message
        }
    }
});

登录服务:

app.service('signInService', function($http) {

    this.authUser = function (usrnm, pwd) {

        return $http({
            url: '/api/json/authenticate',
            method: "POST",
            data: '{}',
            headers: {
                'Content-Type': 'application/json',
                'X-app-Username': usrnm,
                'X-app-Password': pwd
            }
        });
    };
});

单元测试:

describe('mocking service http call', function() {
    beforeEach(module('myApp'));

    var LoginController, $scope;

    describe('with httpBackend', function() {
        beforeEach(inject(function($controller, $rootScope, $httpBackend) {
            $scope = $rootScope.$new();

            $httpBackend.when('POST', '/api/json/authenticate', {}, function(headers) {
            return {
                'Content-Type': 'application/json',
                'X-app-Username': 'admin',
                'X-app-Password': 'admin'
            };
        }).respond(200)

            LoginController = $controller('LoginController', { $scope: $scope });
            $httpBackend.flush();
        }));

        it('should set data to "things and stuff"', function() {
            expect($scope.data).toEqual({things: 'and stuff'});
        });
    });
});

当 运行 测试时,我看到以下错误:mocking service http call » with httpBackend 错误:没有待处理的刷新请求!


控制器服务.succeed:

app.controller('LoginController', function($scope, $http, signInService, cookieSrv) {

    $scope.LogIn = function(usrnm, pwd) {
        signInService.authUser(usrnm, pwd)
        .success(function (data, status, headers, config) {
            // Display success message
            var cookieID = 'myCookie';
            cookieSrv.createCookie(cookieID, data.token, 3, data.redirectUrl);
        })
        .error(function (data, status, headers, config) {
           // Display error message
        }
    }
});

cookieSrv.js

app.service('cookieSrv', function() {
    return {
        createCookie : function(cookieID, token, days, redirectUrl) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
            }
            else var expires = "";
            document.cookie = cookieID+"="+token+expires+"; path=/";

            window.location.assign(redirectUrl)
        }
    }
});

您的控制器在 $scope 上定义了一个登录方法,但您在测试中没有调用此函数,因此没有发出实际的 http 请求。

通过在调用 flush

之前调用 $scope.logIn 来修改测试
LoginController = $controller('LoginController', { $scope: $scope });
$scope.logIn("Test","test");  // Add this
$httpBackend.flush();