2012-01-04 53 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 
} 

但测试时只需要使用它。