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 如何为返回承诺的服务编写测试单元_Angularjs_Unit Testing_Promise_Karma Jasmine_Deferred - Fatal编程技术网

Angularjs 如何为返回承诺的服务编写测试单元

Angularjs 如何为返回承诺的服务编写测试单元,angularjs,unit-testing,promise,karma-jasmine,deferred,Angularjs,Unit Testing,Promise,Karma Jasmine,Deferred,这是我的app.js中的工厂 app.factory('userInfoFacrory', ['$http' , "$q", function($http,$q){ return { getNames:function(){ var differed = $q.defer(); $http.get("http://localhost/ang/api/v1/users/names") .su

这是我的app.js中的工厂

  app.factory('userInfoFacrory', ['$http' , "$q", function($http,$q){
    return {
           getNames:function(){
            var differed  = $q.defer();
            $http.get("http://localhost/ang/api/v1/users/names")
            .success(function(data) {
                differed.resolve(data);
            }).error(function(msg) {
                differed.reject(msg);
            });
            return differed.promise;
          }
    }
  }])
我在控制器中使用此工厂,如bellow,它工作良好:

 app.controller('mainController', ['$scope','userInfoFacrory','$log', function($scope,userInfoFacrory,$log){

    var promise = userInfoFacrory.getNames();
    promise.then(function (data) {
        $log.info(data); // I get my data correctly here
    }, function (msg) {
        $log.error(data);
    })
}])
在这里,我试着用karma jasmine编写一个测试单元

describe('userInfoFacrory', function() {
var  factory ,$rootScope,$scope,$q,onTaskComplete , promise;

       beforeEach(function() {
        module("testApp");
        inject(function ($injector) {   
            $q = $injector.get("$q");
            factory = $injector.get("userInfoFacrory");
            $rootScope = $injector.get("$rootScope");
            $scope = $rootScope.$new();
            promise = factory.getNames(); // this function comes from my factory which returns a promise 
        });      
      });

      it('should return a promise', function() {
          // This test will pass , so no error so far 
         expect(typeof promise.then).toEqual('function');
      });
});
但我不知道如何进行测试,所以如果我的承诺中包含我的数据(来自我的api),我们将不胜感激

谢谢

工作许可证:

请注意,您的代码是正确的,但并没有真正利用承诺的链接功能。它可以简单地写成

getNames: function() {
    return $http.get("http://localhost/ang/api/v1/users/names")
        .then(function(response) {
            return response.data;
        }, function(response) {
            return $q.reject(response.data);
        });
    };
}

正在工作的plunkr:

错误:意外请求:不需要更多请求,$httpBackend在这里究竟做了什么?我的意思是我想在我的工厂中运行api调用,看看它是否有效,我不想在单元测试中调用我的api,你应该在beforeach()中删除对服务的调用$httpBackend在文档中有描述:
getNames: function() {
    return $http.get("http://localhost/ang/api/v1/users/names")
        .then(function(response) {
            return response.data;
        }, function(response) {
            return $q.reject(response.data);
        });
    };
}