2012-01-04 102 views
2

我使用jQuery 1.5和Q-Unit來測試。單元測試時防止document.ready()函數

我工作的應用程序具有充當應用程序亞軍文件,具有待機功能和其他一些東西:

-Main.js-

... 
var foo = new Object(); 
foo.bar = function() { 
    /* function I'd like to test */ 
} 
foo.ready = function() { 
    /* sets up the app. I don't want this to run in my test! */ 
} 
$(document).ready(function(){ 
    foo.ready(); 
}); 
... 

運行準備就緒()函數創建了一堆我不想在我的單元測試中浮動的對象,但我仍然需要在Main.js中測試foo.bar()和其他函數。我可以重寫ready()回調嗎?或者總體上有更好的解決方案嗎?謝謝!

+2

http://api.jquery.com/jQuery.holdReady/ – Esailija 2012-01-04 21:21:21

+0

很好。這是一個很棒的功能。我實際上已經準備好明天升級到1.7,所以這應該很好。謝謝! – stinkycheeseman 2012-01-04 21:44:33

回答

2

你可以這樣做,jQuery的文件被加載後,你可以檢查其單元測試,然後將此

$.fn.ready = function() { 
//any code here, or leave it blank and all ready method will call this method instead jQuery one 
} 

但測試時只需要使用它。