2013-10-27 64 views
2

我很難了解茉莉花spyOn功能。 我寫了一個簡單的功能測試,如果我的方法被稱爲:茉莉花預期的間諜myLinks被稱爲錯誤

function myView() { 
    myLinks(); 
} 

這裏是我的測試:

describe('#myView', function() { 
    it('updates link', function() { 
     var spyEvent = spyOn(window, 'myLinks'); 
     expect(spyEvent).toHaveBeenCalled(); 
    }); 
    }); 

這將返回以下故障:

Expected spy myLinks to have been called 

我在做什麼這裏錯了嗎?

回答

5

您需要調用myView()函數,以便調用myLinks()

function myLinks(){ 
    //some tasks 
}  

function myView() { 
    myLinks(); 
} 

這種雙功能上面在窗口對象申報,然後創建指向窗口間諜對象。

describe('#myView', function() { 
    myView();//Call the method so the myLinks was called too 
    it('updates link', function() { 
     var spyEvent = spyOn(window, 'myLinks'); 
     expect(spyEvent).toHaveBeenCalled(); 
    }); 
    }); 
+0

arrgggh!謝謝@Claudio – Micheal