returns 承诺未解决的 Jasmine 单元测试 angular 服务

Jasmine unit testing angular service that returns promise doesn't resolve

鉴于此测试代码

it('can login', inject(function ($httpBackend,$rootScope) {
        // Set up the mock http service responses
        authRequestHandler = $httpBackend.when('POST', '/login')
            .respond({success: true, user: {email: 'david@blah.com', roles: ['user']}});
        var promise = dsAuth.authenticateUser('123', '123')

        promise.then(function (success) {
            console.log('Got login response');
            expect(success).toBe(true);
            expect(dsIdentity.isAuthenticated()).toBe(true);
            console.log(dsIdentity.currentUser);
        });
        $rootScope.$digest(); //a solution found in on SO that doesn't work
    }));

那个承诺(从身份验证服务返回)永远不会解决?这怎么能解决? .then() 函数中的代码永远不会被调用

服务代码:

(function(angular) {
angular.module('dsApp').factory('dsAuth',
    ['$http','$q',dsAuth]);
function dsAuth($http,$q) {
    return {
        authenticateUser: function(username,password) {
            var dfd = $q.defer();
            $http.post('/login', {username: username, password: password}).then(function (resp) {
                console.log($resp);
                if (resp.data.success) {
                    var user = new atUser();
                    angular.extend(user, resp.data.user);
                    atIdentity.currentUser = user;
                    dfd.resolve(true);
                } else {
                    dfd.resolve(false);
                }
            });
            return dfd.promise;
        },
        logoutUser: function() {
            var dfd = $q.defer();
            $http.post('/logout', {logout: true}).then(function () {
                atIdentity.currentUser = undefined;
                dfd.resolve();
            });
            return dfd.promise;
        }
    };
}

})(this.angular);

Jasmine 不适用于异步期望。解决方法是使用httpBackend.

flush()函数
it('can login', inject(function ($httpBackend,$rootScope) {
        // Set up the mock http service responses
        authRequestHandler = $httpBackend.when('POST', '/login')
            .respond({success: true, user: {email: 'david@blah.com', roles: ['user']}});
        var promise = dsAuth.authenticateUser('123', '123')
        var success = false;

        promise.then(function (result) {
            console.log('Got login response');
            success = result;
            console.log(dsIdentity.currentUser);
        });
        $httpBackend.flush();
        expect(success).toBe(true);
        expect(dsIdentity.isAuthenticated()).toBe(true);
    }));

我不太确定 dsIdentity 的来源,但我想您可以在自己的代码中弄清楚。模式是相同的——在闭包外创建一个变量,然后在闭包内设置值。 flush() 会触发 promise,然后你就可以开始了。