Question

I'd like to use mockjax with qunit but can't get it to work. Here is a simple test I tried: http://jsfiddle.net/shapeshifta/3YNB2/

asyncTest('Response returns jsonp', function() {
    $.mockjax({
        url: 'http://search.twitter.com/search.json*',
        contentType: 'text/json',
        proxy: 'mocks/twitter.json'
    });
    window.abcdef123456 = function() {
        start();
        ok(true, 'JSONP Callback executed');
    };

    $.ajax({
        url: 'http://search.twitter.com/search.json?q=Javascript&callback=?',
        jsonpCallback: 'abcdef123456',
        dataType: 'jsonp',
        error: function() {},
        complete: function() {}
    });
    $.mockjaxClear();
});

Works like a charm without the mockjax code but does not really work with it... Am I doing soemthing wrong? Tried to use sinon.js but it has some issues with asynchronous test so I wanted to switch to mockjax. But my code just keeps asking twitter for data although mockjax should mock my request...

Any ideas?

Was it helpful?

Solution

I got this working:

asyncTest('Response returns jsonp', function() {

    $.mockjax({
        url: 'http://search.twitter.com/search.json*',
        contentType: 'text/json',
        proxy: 'mocks/twitter.json'
    });

    function onSucceed(data) {
        start();
        ok(true, 'JSONP completed');
    };

    $.ajax({
        url: 'http://search.twitter.com/search.json?q=Javascript&callback=?',
        dataType: 'jsonp',
        success: onSucceed,
        error: function() { alert('error'); },
        complete: function() { $.mockjaxClear(); }
    });
});​
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top