2013-08-26 38 views
0

我是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);});

注:兩個具有相同的名稱

我怎樣才能做到這一點?提前致謝!

+1

是的,你可以做到這一點,但也許你的問題是,你要重寫mySpy? –

+0

雅有點。間諜在beforeEach被稱爲相當在我的描述/它 – Abilash

回答

1

代替

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 
    }); 
+0

不幸的是我不能改變它的角度服務的名稱! – Abilash

+0

我知道這已經死了,但@Abilash,你想要使用這種方法,並且在創建對象之後,構造你的Controller,Service或Directive傳入新的間諜對象*。在創建間諜對象之後,你應該創建一個函數'createController()',它將在'describe'中創建的新對象構建Controller/Service/etc。 – Atticus