最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to invoke $(document).ready(function() {}) in unit testing - Stack Overflow

programmeradmin7浏览0评论

I'm experiencing difficulties trying to invoke document.ready( function() {}) in my unit tests. Suppose I have multiple of them in my javascript file, and one of them called inside a named function i.e.

function myFunction() {
    $(document).ready(function() {
        //...
    });
}

How do I actually invoke them in my unit tests so I can actually test them? I'm using JsTestDriver to unit test my javascripts.

Thanks.

I'm experiencing difficulties trying to invoke document.ready( function() {}) in my unit tests. Suppose I have multiple of them in my javascript file, and one of them called inside a named function i.e.

function myFunction() {
    $(document).ready(function() {
        //...
    });
}

How do I actually invoke them in my unit tests so I can actually test them? I'm using JsTestDriver to unit test my javascripts.

Thanks.

Share Improve this question edited Dec 5, 2009 at 5:28 Rob Hruska 120k32 gold badges169 silver badges192 bronze badges asked Dec 3, 2009 at 3:09 BeraCimBeraCim 2,3478 gold badges50 silver badges78 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 6

If it's a unit test, I'm guessing you check the function outputs when given certain inputs?

Here's my opinion:

You should prepare for the case where document.ready is called and the case where it isn't.

So your unit test should run each function twice - once to simulate a pre-ready call and one to simulate a post-ready call. That is, you should have one run-through where anything that happens on document.ready DOES run, and one run-through where it's just ignored (presumably to be called later on in the lifecycle).

EDIT: Just reread the question and understood it a bit more. You could just override $(document).ready to do what you want it to (which is NOT to wait for the DOMLoaded event to fire, but instead to run the functions immediately). This snippet will replace the $(document).ready function with a function that does exactly that. It should run before any unit tests.

var postReady = true; // or false to ignore the function calls.
jQuery.fn.ready = function(fn)
{
    if(postReady && fn) fn();
}

Example test case:

<html><head><title>whatever</title>
    <script type="text/javascript" src="/JS/jquery-1.3.2.js"></script>

    <script type="text/javascript">
        var postReady = true; // or false to ignore the function calls.
        jQuery.fn.ready = function(fn)
        {
            alert("We stole ready!");
            if(postReady && fn) fn();
        }

        $(document).ready(function()
        {
            alert("The function is called.");
        });
    </script>
</head><body></body>
</html>

You know document.ready... works so just start with calling the functions within it. Ideally, if you just have an init function called by the ready function then you call one function, it does what you need, and you can continue with your tests.

You can take unit testing too far, in this case you need to ask yourself what you are testing, and why. The JQuery document.ready function works, and work well (you know this because it's been tested by many many people).

I would assume the trick would be to, instead of creating an anonymous function, naming one, and using it.

//So instead of this...
$(document).ready(function() {...});

//Do the following
$(document).ready(my_function);

Then you just test my_function and make sure that it is working. Make sure that you test the functions in the order their going to be loaded for an accurate test.

I suggest you to refactor the code. Even if you find a way to call it, it will be hard to understand for other developers.

Also (IMHO, I am not quite sure) you have to call the ready handlers even after the pages ready event was triggered, because if you "install" the ready() handler, if the document.ready event was already trigger, jquery calls that handler immediately (so it never loses that event, even if your code added a handler too late - that is, way after document.ready was still done).

Couldn't you just create a user my_on_read() event ? Or something the like?

Well, in the end, please just take care of ready() events and handlers that will be installed after the document.ready() is already done :)

Part of the answer to this question can be found here.

Below is the sample code to answer this question based on the above answer:

myFunction();
$.readyList[1]();

The index assumes that there is only 1 document.ready function in the source file. Index 0 refers to something else which I believe is info on the browser.

发布评论

评论列表(0)

  1. 暂无评论