2017-05-26 72 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);