我正在使用 jQuery 1.5 和 Q-Unit 进行测试。
我正在开发的应用程序有一个充当应用程序运行程序的文件,其中包含现成的函数和一些其他内容:
-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();
});
...
运行ready()函数会创建一堆我不想在单元测试中 float 的对象,但我仍然需要测试foo.bar()
和Main中的其他函数.js。我可以重写ready()回调吗?或者一般有更好的解决方案吗?谢谢!
最佳答案
你可以这样做,在加载 jQuery 文件后,你可以检查它是否进行单元测试,然后应用它
$.fn.ready = function() {
//any code here, or leave it blank and all ready method will call this method instead jQuery one
}
但只是在测试时使用它。
关于jquery - 在单元测试时阻止 document.ready() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8734168/