Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have multiple functions and a lot of code inside $(document).ready(function()). I am using jasmine to test the functions inside the ready function as well as the code inside ready() but when the test cases inside describe are executed it is not able to access the code inside the ready function.

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

Jasmine test suite

describe("test suite", function(){
    beforeEach(function () {
        testme = false
    });
    it("Test Alert Box Message For Change Modal", function() {
        expect(testme).toEqual(true);
    });
});

Kindly suggest what approach needs to be taken for the above approach.

share|improve this question
3  
Export the code you have there to a module, test the module instead. Having random code in a random document.ready is not a very testable design to begin with. –  Benjamin Gruenbaum Jul 10 '13 at 20:08
    
I hate it when people make references to the test itself in jasmine identifiers. You're not describing the test suite, you're describing the system! And the thing you're describing doesn't test an alert box, it creates one. –  Aaronaught Nov 30 '13 at 18:19
add comment

2 Answers 2

What is inside the ready function is encapsulated in a separate scope from everything globally.

Put your test cases in the same scope to work.

There other approaches to be taken, is to do with closures, wrapping the anonymous function (in the ready event handler), putting it in a variable and then passing it to ready.. And then using that variable to pass to the test suites. It can be complex to explain and implement to someone who doesnt know object oriented javascript..

var testMe;
$(document).ready(function () {
    testMe=function testMe(){
        testVar = true;
    }
});

testMe is now defined. That created a closure. I forgot the names of those techniques, I think i created hoisting..

anyway..an excellent book to read is Effective Javascript, to familiarize your self with good javascript techniques..It isnt like your usual programming language

share|improve this answer
    
XWormX, Thanks for your reply, I get it what you are saying but i can not change my source code to test it. document.ready has its own scope and that's why i am getting undefined reference error. –  makmak Jul 10 '13 at 19:56
    
It doesnt see what you have inside the ready function, cause everything inside isnt subjected globally. It is a different scope. Try to do what is in my update –  XWormX Jul 10 '13 at 19:58
    
XWormX, Our Source code is a production source code and we are not allowed to do any modification with our source code. –  makmak Jul 10 '13 at 20:05
1  
@makmak Then you can not test that code. You will have to refactor that code out into it's own separate module before you can test it. –  mortalapeman Jul 11 '13 at 3:09
add comment

in your code, domready calls an annonymous function. If we had a reference to this function, we could call it.

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

$(document).ready(onReady);

and in your tests

it("Test Alert Box Message For Change Modal", function() {
    var testVar = false;
    onReady() ;
    expect(testVar).toEqual(true);
});

your code is now testable :) namespacing and modules are recommended to make it even better.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.