2016-04-22 50 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中,不可能跟踪本地函数调用。