2017-05-26 62 views
1

我如何窺視MyFunction並在Jasmine 2.0中返回2如何間諜功能並返回茉莉花值

我有下面的代碼裏面shouldPass在第一行錯誤:

Error: function MyFunction(){ return 1; }() method does not exist

這似乎是使用全功能的函數名

MyFile的。 js:

MyFunctionToTest = function(){ 
    return MyFunction() + 1; 
} 

function MyFunction(){ return 1; } 

MyFileSpec.js:

describe("myTest", function(){ 
    it("shouldPass", function(){ 
     spyOn("MyFile", MyFunction).and.returnValue(2); 

     expect(MyFunctionToTest()).toEqual(3) 
    }) 
}) 

回答

0

您正試圖窺探匿名/全局函數。你或許可以用間諜重新定義它。

MyFunction = jasmine.createSpy().and.returnValue(2);