Jquery ajax load of JSON in unit tests

Posted by wmitchell on Stack Overflow See other posts from Stack Overflow or by wmitchell
Published on 2011-01-11T16:35:02Z Indexed on 2011/01/11 16:53 UTC
Read the original article Hit count: 239

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

 });

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about unit-testing