I have multiple functions and a lot of code inside $(document).ready(function())
ID: 646928 • Letter: I
Question
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.
Explanation / Answer
in your code, domready calls an annonymous function. If we had a reference to this function, we could call it.
var> 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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.