2016-04-22 113 views
0

我對Jasmine Testing和Angular框架相當陌生。我目前有一個特殊的情況,我無法從我的Jasmine測試中引用我的服務功能。我的角服務初始化的在Jasmine測試中無法訪問角度服務功能

部分:

var service = { 
checkIfCurrentIsObject: checkIfCurrentIsObject, 
removeFromCurrentObject: removeFromCurrentObject; 
} 

function checkIfCurrentIsObject() { 
      return getCurrent().isObject(); 
     } 

function removeFromCurrentIsObject() { 
     checkIfCurrentIsObject(); 
      return specialFunction(); 
     } 

現在在我的噶測試:

describe('when current object is property', function() { 

      it('verify the requote function is called', function() { 
      spyOn(Service, 'checkIfCurrentIsObject').and.returnValue(true); 
      spyOn(Service, 'removeFromCurrentIsObject').and.callThrough(); 
      spyOn(Service, 'specialFunction'); 
      expect(Service, 'specialFunction').toHaveBeenCalled(); 

        }); 

現在我面臨的問題是,我spyOn不會對我的checkIfCurrentIsObject功能回暖除非我在我的removeFromCurrentIsObject函數中將它指定爲this.checkIfCurrentIsObject。這是否有特別的理由?

回答

0

是的,它應該是

function removeFromCurrentIsObject() { 
    this.checkIfCurrentIsObject(); 
    return this.specialFunction(); 
} 

對他們的間諜。

是被偷窺的Service.checkIfCurrentIsObject方法。在JavaScript中,不可能跟蹤本地函數調用。