How to resolve $q.all promises in Jasmine unit tests?

ε祈祈猫儿з 提交于 2019-11-26 20:08:11

问题


My controller has code like below:

$q.all([qService.getData($scope.id), dService.getData(), qTService.get()])
.then(function (allData) {
  $scope.data1 = allData[0];
  $scope.data2 = allData[1];
  $scope.data3 = allData[2];
});

And in my unit tests i am doing something like this:

beforeEach(inject(function($rootScope, $q, $location){// and other dependencies... 
  qServiceSpy = spyOn(_qService, 'getData').andCallFake(function () {
    var data1 = {
      id: 1,
      sellingProperty: 1,
    };
    var d = $q.defer();
    d.resolve(data1);
    return d.promise;
  });

  dServiceSpy = spyOn(_dService, 'getData').andCallFake(function () {
    var data2 = [{ "id": "1", "anotherProp": 123 }];
    var d = $q.defer();
    d.resolve(data2);
    return d.promise;
  });
  qTServiceSpy = spyOn(_qTService, 'get').andCallFake(function () {
    var data3 = [{ id: 0, name: 'Rahul' }];
    var d = $q.defer();
    d.resolve(data3);
    return d.promise;
  });
  rootScope = $rootScope;
});

Now in my test i am checking if services are called and the data1, data2 are not undefined..

it('check if qService' got called, function() {
  expect(scope.data1).toBeUndefined();
  rootScope.$digest();
  expect(_quoteService.getQuote).toHaveBeenCalled();
});
it('check if "data1" is defined', function () {
  expect(scope.data1).toBeUndefined();
  rootScope.$digest();
  expect(scope.data1).toBeDefined();
});

my problem is, this was working fine until i replaced my individual service calls in controller with q.all and in tests scope.$apply with rootScope.$digest. With q.all and rootScope.$digest (tried using scope.$apply as well) both tests fails with error:

10 $digest() iterations reached. Aborting!

if i remove rootScope.$digest then the promises never get resolves and tests fails saying

expected undefined to be defined.

Any help how should i unit tests code with q.all?

came across this post

But that also doesn't help as i am already trying to use $digest.


回答1:


You can try putting $rootScope.$apply() in an afterEach() callback function. Promises resolve on $apply() in Angular.

afterEach(function(){
    rootScope.$apply();
});


来源:https://stackoverflow.com/questions/16316940/how-to-resolve-q-all-promises-in-jasmine-unit-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!