如何为返回promise的服务编写测试单元

时间:2015-07-04 15:08:24

标签: 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")
            .success(function(data) {
                differed.resolve(data);
            }).error(function(msg) {
                differed.reject(msg);
            });
            return differed.promise;
          }
    }
  }])

我在我的控制器中使用这个工厂,如波纹管,它工作正常:

 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),任何建议都会受到赞赏。

感谢

1 个答案:

答案 0 :(得分:2)

it('should return a promise resolved with the http response data if the http request is successful', inject(function($httpBackend) {

    var expectedData = 'fake data';
    $httpBackend.expectGET('http://localhost/ang/api/v1/users/names').respond(expectedData);
    var promise = factory.getNames();
    var actualData;
    promise.then(function(result) {
        actualData = result;
    });
    $httpBackend.flush();
    expect(actualData).toEqual(expectedData);
}));

it('should return a promise rejected with the http response data if the http request is in error', inject(function($httpBackend) {

    var expectedData = 'fake data';
    $httpBackend.expectGET('http://localhost/ang/api/v1/users/names').respond(400, expectedData);
    var promise = factory.getNames();
    var actualData;
    promise.catch(function(result) {
        actualData = result;
    });
    $httpBackend.flush();
    expect(actualData).toEqual(expectedData);
}));

工作plunkr:http://plnkr.co/edit/NfO6KXWLs1QT5HG8MK0J?p=preview

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

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:http://plnkr.co/edit/C5x8wRYCQ0wetjozEd0a?p=preview

相关问题