lundi 2 mai 2016

When testing AJAX calls in JS with Jasmine, what is the difference between .andReturn(...) and .respondWith(...)?

When testing JavaScript code with the jasmine-ajax library, I can mock ajax responses. In particular, I can define the response that will be given to a particular ajax request. It seems that there are (at least) two different ways of doing this:

Approach #1:

jasmine.Ajax.requests.mostRecent().respondWith({
  status: 200,
  contentType: 'text/plain',
  responseText: 'my response'
});

Approach #2:

jasmine.Ajax.stubRequest('my/url').andReturn({
  'responseText': 'my response'
});

If my mostRecent request is to the url my/url, then what else is the difference between these two?

I've been introduced to these two approaches in the "JavaScript Unit Testing" video series by M.E. Trostler, but I can't find a clear answer to my question in those videos, in a search of StackOverflow or in the Jasmine online documentation for its ajax.js plugin.

Aucun commentaire:

Enregistrer un commentaire