$ rootScope.digest没有在Jasmine中完成一个承诺

Bre*_*anM 9 javascript jasmine angularjs angular-promise

我们目前正在尝试测试我们的角度服务,这些服务使用promises将值返回给控制器.问题是我们附加到.then的函数不会在Jasmine中被调用.

我们发现在返回promise之后向函数添加$ rootScope.digest()允许调用同步promises但是它仍然不适用于异步promise.

到目前为止的代码是

    beforeEach(inject(function (Service, $rootScope)
    {
        service = Service;
        root = $rootScope;
    }));

    it('gets all the available products', function (done)
    {
        service.getData().then(function (data)
        {
            expect(data).not.toBeNull();
            expect(data.length).toBeGreaterThan(0);
            done();
        });
        root.$digest();
    });
Run Code Online (Sandbox Code Playgroud)

在这种情况下,promise被称为罚款,但是如果它是异步的,则不会被调用,因为在根目录下,$ digest()被称为"消化".

是否有一些方法可以告诉我什么时候得到解决,以便我可以调用摘要?或者也许会自动做到这一点?谢谢 ;)

我们必须测试部分服务(删除错误处理):

var app = angular.module('service', []);

/**
 * Service for accessing data relating to the updates/downloads
 * */
app.factory('Service', function ($q)
{
     ... init

    function getData()
    {
        var deffered = $q.defer();

        var processors = [displayNameProc];

        downloads.getData(function (err, data)
        {
            chain.process(processors, data, function (err, processedData)
            {
                deffered.resolve(processedData);
            });
        });

        return deffered.promise;
    }
    ...
Run Code Online (Sandbox Code Playgroud)

在出现问题的情况下,当service.getData是异步时,promise被解析,但是由于已经调用了根.$ digest,因此不会调用从测试代码附加到该promise的函数.希望这能提供更多信息

解决方法

var interval;
beforeEach(inject(function ($rootScope)
{
    if(!interval)
    {
        interval = function ()
        {
            $rootScope.$digest();
        };
        window.setInterval(interval, /*timing*/);
    }
}));
Run Code Online (Sandbox Code Playgroud)

我最后使用上面的解决方法反复调用摘要,因此每一层承诺都有机会运行...它不理想或漂亮但它适用于测试......

Daa*_*lst 1

您应该将您的断言移出我认为的范围之外:

beforeEach(inject(function (Service, $rootScope)
{
    service = Service;
    root = $rootScope;
}));

it('gets all the available products', function (done)
{
    var result;
    service.getData().then(function (data)
    {
        result = data;
        done();
    });

    root.$digest();

    expect(result ).not.toBeNull();
    expect(result .length).toBeGreaterThan(0);
});
Run Code Online (Sandbox Code Playgroud)

更新

plunkr从下面的评论中分叉出来,以展示如何测试异步调用。

向服务添加了 $timeout:

var app = angular.module('bad-service', []);

app.service('BadService', function ($q, $interval, $timeout)
{
    this.innerPromise = function(){
      var task = $q.defer();
      console.log('Inside inner promise');

      $timeout(function() {
        console.log('Inner promise timer fired');
        task.resolve();
      }, 500);
      // $interval(function(){

      // }, 500, 1);
      return task.promise;
    }
});
Run Code Online (Sandbox Code Playgroud)

我在断言之前刷新 $timeout :

beforeEach(module('test'));


describe('test', function(){
  var service, root, $timeout;

    beforeEach(inject(function (Service, $rootScope, _$timeout_)
    {
      $timeout = _$timeout_;
        console.log('injecting');
        service = Service;
        root = $rootScope;
    }));

    /**
    Test one
    */
    it('Should work', function(done){
      console.log('Before service call');
      service.outerPromise().then(function resolve(){
        console.log('I should be run!');
        expect(true).toBeTruthy();
        done();
      });
      // You will have to use the correct "resolve" here. For example when using:
      // $httpbackend - $httpBackend.flush();
      // $timeout- $timeout.flush();
      $timeout.flush();

      console.log('After service call');
    });
});
Run Code Online (Sandbox Code Playgroud)