ash123
ash123

Reputation: 307

How to unit test a document.ready() function using jasmine

How do you test document.ready block using Jasmine? To be more specific , if I have a block like this :

$(document).ready(
    function () {
        abc = true;
    }
});

How do you test that the inner function was called when the document was ready, using Jasmine?

Upvotes: 5

Views: 7770

Answers (2)

Elena
Elena

Reputation: 1829

You could refactor your code to be something like this:

var onReady = function(){
    abc = true;
}

$(document).ready(onReady);

and your tests:

it("Tests abc", function() {
    onReady() ;
    expect(tesabctVar).toEqual(true);
});

Upvotes: 5

webcodervk
webcodervk

Reputation: 145

How do you test document.ready block using Jasmine? To be more specific , > if I have a block like this :

$(document).ready( function(){ abc= true; } );

My understanding is that code you have written within $(document).ready closure above is not testable. This link has a good explanation of how to make it more testable : http://bittersweetryan.github.io/jasmine-presentation/#slide-17

How do you test that the inner function was called when the document was ready, using Jasmine?

Answered above by m59 in comment already.

Upvotes: 3

Related Questions