Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 返回承诺的Jasmine单元测试服务无法解决_Angularjs_Unit Testing_Jasmine_Karma Runner - Fatal编程技术网

Angularjs 返回承诺的Jasmine单元测试服务无法解决

Angularjs 返回承诺的Jasmine单元测试服务无法解决,angularjs,unit-testing,jasmine,karma-runner,Angularjs,Unit Testing,Jasmine,Karma Runner,给出了这个测试代码 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.co

给出了这个测试代码

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

}这是有棱角的

Jasmine不适用于异步预期。解决方法是使用httpBackend的flush函数


我不确定dsIdentity从何而来,但我想您可以在自己的代码中找到它。模式是相同的-在闭包外部创建一个变量,然后在闭包内部设置值。同花顺会导致诺言被点燃,然后你就可以出发了

谢谢,很多人没有意识到它不是异步兼容的
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);
    }));