Javascript 当单元测试角度JS Promise';s notify函数,从不调用notify

Javascript 当单元测试角度JS Promise';s notify函数,从不调用notify,javascript,angularjs,promise,mocha.js,notify,Javascript,Angularjs,Promise,Mocha.js,Notify,我有一个函数,在发生事情时使用notify更新UI: promiseFunction.poll().then( function resolve() {}, function reject() {}, function notify() { console.log('Notify was called'); } ); 还有一个单元测试试图模拟notify it('will call notify', function () { sinon.stub

我有一个函数,在发生事情时使用notify更新UI:

promiseFunction.poll().then(
   function resolve() {},
   function reject() {},
   function notify() {
       console.log('Notify was called');
   }
);
还有一个单元测试试图模拟notify

it('will call notify', function () {
    sinon.stub(promiseFunction, 'poll', function () {
        var pollDeferred = $q.defer();
        pollDeferred.notify();
        return pollDeferred.promise;
    });

    systemUnderTest.run();
    $rootScope.$digest();

});

从未记录日志“Notify was called”。为什么notify函数不启动?

好的,所以我最终解决了这个问题。想想看,这是有道理的。Notify无法解析,因此无法履行承诺,因此无法在承诺返回给调用方之前发送$超时在这里起到了解救作用,因此最终生成的单元测试代码是:

it('will call notify', inject(function ($timeout) {
    sinon.stub(promiseFunction, 'poll', function () {
        var pollDeferred = $q.defer();

        $timeout(function () {
            pollDeferred.notify();
        }, 0);

        return pollDeferred.promise;
    });

    systemUnderTest.run();
    $timeout.flush();
    $rootScope.$digest();
}));

如果改为调用$rootScope.$apply(),会发生什么情况?同样的情况也会发生。我现在已经解决了,我会补充答案