如何解决 Jasmine 单元测试中的 $q.all 承诺? [英] How to resolve $q.all promises in Jasmine unit tests?

查看:18
本文介绍了如何解决 Jasmine 单元测试中的 $q.all 承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器有如下代码:

$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];});

在我的单元测试中,我正在做这样的事情:

beforeEach(inject(function($rootScope, $q, $location){//和其他依赖...qServiceSpy = spyOn(_qService, 'getData').andCallFake(function () {变量数据1 = {编号:1,出售物业: 1,};var d = $q.defer();d.解析(数据1);返回 d. 承诺;});dServiceSpy = spyOn(_dService, 'getData').andCallFake(function () {var data2 = [{ "id": "1", "anotherProp": 123 }];var d = $q.defer();d.解析(数据2);返回 d. 承诺;});qTServiceSpy = spyOn(_qTService, 'get').andCallFake(function () {var data3 = [{ id: 0, name: 'Rahul' }];var d = $q.defer();d.解决(数据3);返回 d. 承诺;});rootScope = $rootScope;});

现在在我的测试中,我正在检查是否调用了服务,并且 data1、data2 不是未定义的..

it('check if qService' 被调用,function() {期望(scope.data1).toBeUndefined();rootScope.$digest();期望(_quoteService.getQuote).toHaveBeenCalled();});it('检查是否定义了data1"', function () {期望(scope.data1).toBeUndefined();rootScope.$digest();期望(scope.data1).toBeDefined();});

我的问题是,这一直工作正常,直到我用 q.all 替换了控制器中的各个服务调用,并用 rootScope.$digest 替换了测试 scope.$apply.使用 q.all 和 rootScope.$digest(也尝试使用 scope.$apply),两个测试都失败并显示错误:

<块引用>

达到 10 次 $digest() 迭代.正在中止!

如果我删除 rootScope.$digest 那么承诺永远不会得到解决并且测试失败说

<块引用>

预期未定义.

任何帮助我应该如何使用 q.all 对代码进行单元测试?

偶然发现这篇文章>

但这也无济于事,因为我已经在尝试使用 $digest.

解决方案

您可以尝试将 $rootScope.$apply() 放在 afterEach() 回调函数中.Promise 在 Angular 中的 $apply() 上解析.

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

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.

解决方案

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

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

这篇关于如何解决 Jasmine 单元测试中的 $q.all 承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆