简体   繁体   中英

Does mockjax provide more functionality then sinon.js for faking ajax call?

I can fake ajax with both of these tools. Sinon allow to create stub/spy/mock that come handy for testing and mockjax doesn't. When it come to faking ajax call, does mockjax provide more features then Sinon? Cause if it doesn't, there is no point is using both of them.

var response = [];
var statusCode = 200;
var responseTime = 0;

Example how to fake ajax call with mockjax :

$.mockjax({
    url: server_api_url + "/Something/GetData",
    status: statusCode,
    responseTime: responseTime,
    contentType: "application/json",
    responseText: response
});

Example how to fake ajax call with Sinon.js :

var def = $.Deferred();
var stubGetData = sinon.stub(serverApiForSomething, "GetData");
def.resolve(response);
stubGetData.returns(def);

Where serverApiForSomething is a global class that encapsulate ajax call.

ServerApiForSomething = function()
{
    var self = this;
    self.GetData = function(param)
    {
        var ajaxOption = 
        {
            url:server_Api_Url + "/Something/GetData",
            type: "GET",
            data: { param.toJSON() },
            contentType: "application/json",
            dataType: "json"
        }
        return $.ajax(ajaxOption);
    }
}

serverApiForSomething = new ServerApiForSomething();

I'm the maintainer of the Mockjax plugin. I would agree with @AXMIM's comments that Mockjax has some cool built-in features specifically for mocking ajax calls, but it also is only intended to be used with jQuery, where as Sinon can be used with any framework (but doesn't have quite as many built-in cool things).

That said, looking at your code, you are not really mocking an Ajax call with that Sinon code, you're mocking out the GetData method. That's cool too, but not really the point of Mockjax, which is take the Ajax call out of the picture but leave everything else in place. The proper comparison is using Sinon's FakeServer feaure .

Really, you can use both, if you like the syntax of Mockjax better, then use that for mocking Ajax, and use Sinon to spy on the success and error handlers.

Draft answer
While both can fake ajax call, mockjax provide more build-in feature. However, those feature are really about "ajax call" and are quite too much if you only need to fake ajax call with basic "succeeded"/"failed" behaviour.

For example, with mockjax, you can

  • set the response status "404, 200, 500, etc".
  • randomize the time it's take to response
  • timeout the call
  • etc

See mockjax documentation here
With mockjax you can also find all ajax call that were made without being faked. This is really usefull.

So, unless you need to fake more then failure/success and the value returned, sinon is quite enough and easier to use as you don't need to bother with route and other related stuff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM