【问题标题】:Jasmine Unit Test Promise function that is called multiple times based on the response from another promise functionJasmine 单元测试 Promise 函数,根据另一个 Promise 函数的响应多次调用
【发布时间】:2016-09-03 12:07:06
【问题描述】:

我想对一个根据另一个函数的响应多次调用的函数进行单元测试。

与我的问题类似:Call a promise function multiple times until condition met from another promise function。

这里是函数

var monitorProgress = function() {  
    return getActiveTasks().then(function(res) { 
        if(res.length > 0) {
            progress = res[0].progress;
            if(progress !== 100){
                return $timeout(function(){
                    return monitorProgress();  // multiple calls
                },1000);
            }
            else {
                // exit
                console.log("done"); 
            }
        }
        else{
            console.log("done"); 
        }

    });  
};

【问题讨论】:

    标签: angularjs unit-testing jasmine promise q


    【解决方案1】:

    这是我测试此功能的方法:

    describe('monitorProgress function', function() {
        var response = null;
        var promise = null;
        var progress = 0;
        beforeEach(function() {   
    
            // fake the implementation of monitorProgress
            spyOn(SyncService,'monitorProgress').and.callFake(function() {
    
                progress++;
    
                // this mimics getActiveTasks promise and 
                // returns a promise with a res array length > 0
                var deferred = $q.defer();
                var res = [{ progress : progress }]; 
                deferred.resolve(res);
    
                // here the promise is returns 
                return deferred.promise.then(function(res) {
                    console.log("getActiveTasks res: " + JSON.stringify(res,null,2))
                    if(res.length > 0) {
                        progress = res[0].progress;
                        if(progress !== 100){
                            // keeps calling this until progress === 100
                            return SyncService.monitorProgress();
                        }
                        else {
                            // exit
                            console.log("exiting"); 
                        }
                    }
                    else {
                        // we never go here
                    }
                });  
            });
    
            // now that the spy is set, call the function
            promise = SyncService.monitorProgress(); 
        }) 
    
        it("tracks that monitorProgress was called", function() {
            expect(SyncService.monitorProgress).toHaveBeenCalled();
        });
    
        it("tracks that monitorProgress returned a promise", function() {
            expect(promise).toBeDefined();
        });
    
        it("should return when progress is 100", function() { 
            // very important step
            $rootScope.$apply();
    
            // the final expectation
            expect(progress).toBe(100);
        }); 
    }); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-11
      • 2017-05-13
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      相关资源
      最近更新 更多