Jquery ajax load of JSON in unit tests
- by wmitchell
I'm trying to load a dataset in jasmine for my tests like such ...
However as its a json call I cant seem to always get the test denoted by "it" to wait till the JSON call has finished before using its array. I tried using the ajaxStop function to no avail. Any ideas ?
describe("simple checks", function() {
var exampleArray = new Array();
beforeEach(function(){
$(document).ajaxStop(function() {
$(this).unbind("ajaxStop");
$.getJSON('/jasmine/obj.json', function(data) {
$.each( json.jsonattr, function(i, widgetElement) {
exampleArray.push(new widget(widgetElement));
});
});
});
});
it("use the exampleArray", function() {
doSomething(exampleArray[0]); // frequently this is coming up as undefined
});