Lorraine Bernard
Lorraine Bernard

Reputation: 13400

Mock the calls to the server by using jasmine

I would like to mock the ajax calls to the server by using jasmine and also test the done and fail Deferred Object.

Currently, I am doing them for real, thus trying to send a bunch of call to the server.

How should I fix the following code to

mySpy = spyOn(backendController, 'submitForm').andCallThrough(); 
// it makes a real call to the server

mySpy = spyOn(backendController, 'submitForm'); 
// it does not make a real call to the server but I get the following error
// Cannot call method 'done' of undefined

Here is the code about the doSubmitForm

doSubmitForm: function (backendController) {
  backendController.submitForm(message.val())
        .done(this.onSuccess)
        .fail(this.onError);
});

Upvotes: 3

Views: 2177

Answers (1)

Karthik
Karthik

Reputation: 1006

In the failing case, I think the problem is due to the call not returning a jQuery-Deferred object.

To validate this theory, you could probably try something like this:

var tmpDefObj = $.Deferred();

spyOn(backendController, 'submitForm').andCallFake(function() {return tmpDefObj;});

Upvotes: 3

Related Questions