1

Problem

I'm using QUnit, and following this stackoverflow answer which seems to work in that scenario.

The data I attempt to supply to the 'success' method doesn't 'arrive' - it's not in the format required, or comes through as undefined. I can't see where I'm going wrong.

Walk-through

The 'unitTest' method that supplies my 'methodUnderTest' with a DOM element. The 'methodUnderTest' makes an jQuery ajax call, which is mocked in the unit test. Then success is called, and the result of the success method executing forms the unit test assert.

Code

I have set this up as a JSFiddle example here, if you would like to see the code executing (failing to execute as expected). But here is some of it, in case my mistake is very obvious:

//stubbing the ajax call
jQuery.ajax = function (param) {
    options = param;
};

//the data
var expectedData = jQuery.parseJSON({
        'id': 'fbs',
        'fieldName': 'fbs',
        'data': 'fbs-text'
    });

//call to success method, in unit test
options.success({
    sdata: expectedData,
    textStatus: 'is-this-data-arriving',
    jqXhr: 123
});

//the success method that is failing to get the expected data
function handleSuccess (sdata, textStatus, jqXhr) {
    //problem is here not this success method never gets the data
    console.log('sdata: ' + sdata + '|textStatus:' + textStatus + '|jq:' + jqXhr);
    jQuery('#' + sdata.fieldName + '-loading').attr('alt', 'modified');
};
Community
  • 1
  • 1
Nick Josevski
  • 4,156
  • 3
  • 43
  • 63

1 Answers1

1

Ok, there was a few mistakes that were interfering with each other. This was a pretty localized issue, but with the working code here now if someone reaches a similar problem hopefully this will be helpful.

Working solution

http://jsfiddle.net/NickJosevski/9ZZmc/4/

Correct Code Snippets

//1.
//making the parseJSON method work correctly by supplying a single string
//as per http://api.jquery.com/jQuery.parseJSON/

var jsonText = '{"id": "fbs", "fieldName": "fbs", "data": "fbs-text" }'),
    expectedData = $.parseJSON(jsonText);

//2.
//passing the values to the success method correctly
var ignoreParamForThisDemo = null;
options.success(expectedData, ignoreParamForThisDemo , ignoreParamForThisDemo );

//3.
//Other minor issues (broken assert logic) that can be seen in the final solution
Nick Josevski
  • 4,156
  • 3
  • 43
  • 63
  • If you need more context/code examples, I've blogged more info here - http://blog.nick.josevski.com/2012/01/05/unit-testing-javascript-methods-that-contain-jquery-ajax-calls/ – Nick Josevski Jan 05 '12 at 10:43