Angularjs 使用Jasmine angular js测试真正的https post调用

Angularjs 使用Jasmine angular js测试真正的https post调用,angularjs,unit-testing,https,jasmine,Angularjs,Unit Testing,Https,Jasmine,我有以下服务和控制器,将数据插入数据库。模块应用程序已经定义,没有Jasmine,一切正常 app.service('registerService', function($http, $q) { var deferred = $q.defer(); this.register = function(userDetails) { /* TODO: Populate the userId in the fly */ var dataPart = {

我有以下服务和控制器,将数据插入数据库。模块应用程序已经定义,没有Jasmine,一切正常

app.service('registerService', function($http, $q) {
    var deferred = $q.defer();
    this.register = function(userDetails) {
        /* TODO: Populate the userId in the fly */
        var dataPart = {
            userName : userDetails.userName,
            firstName : userDetails.firstName,
            lastName : userDetails.lastName,
            password : userDetails.password,
            email : userDetails.email,
            phone : userDetails.phone
        };
        return $http.post('/phase1/blogger/user/addUser/', dataPart)
            .then(function(response) {
                deferred.resolve(response.data);
                return deferred.promise;
            }, function(response) {
                // the following line rejects the promise 
                deferred.reject(response);
                return deferred.promise;
            });
    };
});
下面是控制器

app.controller('registerController', function($scope, $location, registerService, validationService) {
    $scope.newUserData = '';
    $scope.temp = '';
    $scope.registerUser = function(userDetails) {       
        $scope.error = '';
        if (userDetails != null && userDetails.firstName != null && userDetails.userName != null &&
                userDetails.password != null && userDetails.email != null && userDetails.phone != null) {       
            if (validationService.validatePhone(userDetails.phone)) {
                    // Validate Password and Confirm Password 
                if (validationService.validatePassword(userDetails.password, userDetails.confirmPassword)) {
                    $scope.temp = 'dee2';
                    var c = JSON.parse(JSON.stringify(userDetails));
                    $scope.temp = 'after stringify';
                    registerService.register(c)
                        .then(function(data) {
                            $scope.newUserData = data;
                            $scope.temp = 'dee3';
                            $location.path('/');
                        }, function(error) {
                            $scope.temp = 'dee3';
                            alert("Registration Failed");
                            $location.path('/');
                        });
                } else {
                    $scope.error = 'Password and Confirm Password donot match';
                }
            } else {
                $scope.error = 'Enter a valid Phone number';
            }
        } else {
            $scope.error = 'Mandatory field UserName or FirstName or Password or Email or Phone empty';
        }
    };
    $scope.registerCancel = function() {
        $location.path('/');
    };

});
下面是JASMINE测试代码

describe('new App Test', function () {
    beforeEach(function() {
        module('newDirectiveApp');
        module('newApp');
    });

    var $controller;
    var $httpBackend;

    beforeEach(inject(function(_$controller_, _$httpBackend_) {
        $controller = _$controller_;
        $httpBackend = _$httpBackend_;
    }));

    describe('Test User Registration', function() {

        it("should register a new user", function() {
            var $scope = {};
            var controller = $controller('registerController', { $scope: $scope });

            var details = {};
            details.firstName = 'new1';
            details.lastName = 'new1Last';
            details.userName = 'new1';
            details.password = '123';
            details.confirmPassword = '123';
            details.email = 'new1@gmail.com';
            details.phone = '9986513765';
            $scope.registerUser(details);
            var dataPart =  {   
                "userName" : "new1",            
                "firstName" : "new1",
                "lastName" : "new1Last",
                "password" : "123",
                "email" : "new1@gmail.com",
                "phone" : "9986513765"
             };

            expect($scope.temp).toEqual('newDee');
        });
    });        
});
当$scope.registerUser(详细信息)出现时;调用时,代码到达$scope.temp='after stringify';在“注册表控制器”中。 但是它没有达到$scope.temp='dee3'; 我可以知道原因吗?
提前感谢。

几个原因:在单元测试中,$httpBackend被一个假的$httpBackend替换$http是异步的,因此您不能期望它的响应在发送请求后立即可用。顺便说一句,你也误用了承诺。阅读并测试我是否应该通过提供虚假响应来模拟$http.post('/phase1/blogger/user/addUser/',dataPart)?是的,如模拟$httpBackend的文档中所述。