Reputation: 28617
I have a method that when called, makes a HTTP request to the server, and does not care about what happens after. The response is not parsed, and it does not change its state after sending the request or receiving the response.
Now I wish to test this method, ans since there is no state change, I cannot verify that the HTTP request has been made by inspecting the method or the object that owns it. I somehow need to attach sinon to $.mockjax
such that I can perform an assertion verifying that the HTTP request was indeed made.
How to do this?
Upvotes: 1
Views: 670
Reputation: 13273
You can use the $.mockjax.mockedAjaxCalls()
method to retrieve an array of the calls that Mockjax has intercepted. The array will contain the requestSettings
object for each request which you can inspect if necessary:
QUnit.test('two calls should be made', function(assert){
// set up the mock
$.mockjax({ ... });
$.ajax({ ... }); // matching ajax call
$.ajax({ ... }); // NON-matching ajax call
$.ajax({ ... }); // another matching ajax call
assert.equal($.mockjax.mockedAjaxCalls().length, 2, 'only two mockjax calls are recorded');
});
Upvotes: 2