茉莉花 – 同样方法的两个间谍

我是Jasmine新手,我想知道我们是否可以使用相同的方法创建2个间谍。 这是我正在尝试的。

 describe('something', function () { beforeEach(function () { mySpy = jasmine.createSpyObj('mySpy', 'functionInInterest'); mySpy.functionInInterest.andCallFake(function (cb) {cb(something);}); } //Some Test Cases describe('Here is the action!', function () { mySpy = jasmine.createSpyObj('mySpy', 'functionInInterest'); mySpy.functionInInterest.andCallFake(function (cb) {cb(somethingElse);}); //Some test cases that depends on somethingElse }); }); 

之前的测试用例Here is the action! 取决于mySpy.functionInInterest.andCallFake(function (cb) {cb(something);}); 里面的测试用例Here is the action! 取决于mySpy.functionInInterest.andCallFake(function (cb) {cb(somethingElse);});

注意:两者都具有相同的名称

我怎样才能做到这一点? 提前致谢!

代替

 describe('Here is the action!', function () { mySpy = jasmine.createSpyObj('mySpy', 'functionInInterest'); mySpy.functionInInterest.andCallFake(function (cb) {cb(somethingElse);}); //Some test cases that depends on somethingElse }); 

做这个

 describe('Here is the action!', function () { mySpy_2 = jasmine.createSpyObj('mySpy', 'functionInInterest'); mySpy_2.functionInInterest.andCallFake(function (cb) {cb(somethingElse);}); //Some test cases that depends on somethingElse });