Callback not firing from the sinon.js fakeServer for a call to $.ajax
-
28-10-2019 - |
سؤال
I have the following jasmine spec:
describe "plugins", ->
beforeEach ->
@server = sinon.fakeServer.create()
afterEach ->
@server.restore()
describe "reviewStatus", ->
it "should attach dates to content", ->
@server.respondWith("GET", "/GeneralDocument.mvc.aspx/GetDocumentParent?typeName=ncontinuity2.core.domain.Plan&documentParentUid=45f0bccb-27c9-410a-bca8-9ff900ab4c28d",
[200, {"Content-Type": "application/json"},
'{"ReviewDate":"22/09/2012","AcknowledgedDate":"05/07/2012"}'])
$('#jasmine_content').addReviewStatus('ncontinuity2.domain.Plan', "45f0bccb-27c9-410a-bca8-9ff900ab4c28")
@server.respond()
expect($('#reviewDateTab').find("strong").eq(0).length).toEqual(1)
The addReviewStatus is a jQuery plugin I have written:
do($ = jQuery) ->
$.fn.extend
addReviewStatus: (type, uid) ->
ele = @
reviewData = null
getJSON '/GeneralDocument.mvc.aspx/GetDocumentParent', {typeName: type, documentParentUid: uid},
(document) ->
console.log('document = ' + document)
compileTemplate(ele, document)
(response) ->
showErrorMessage resonse.responseText
#etc., etc.
The getJSON method above calls $.ajax like this:
function getJSON(url, params, ajaxCallBack, ajaxErrorHandler, excludeProgress){
var e = (ajaxErrorHandler) ? ajaxErrorHandler : validationErrorCallBack;
var s = (ajaxCallBack) ? ajaxCallBack : jsonCallBack;
$.ajax({
type: "GET",
url: url,
cache: false,
data: params,
beforeSend: function(xhr) {
xhr.setRequestHeader("Ajax", "true");
xhr.setRequestHeader("UseAjaxError", "true");
},
complete: function() {
},
success: s,
timeout: _ajaxTimeOut,
dataType: "json",
error: e
});
}
The anonymous function callback of the getJSON method is not being fired. Also the call to $.ajax is returning a 404 not found. Can anyone see what I am doing wrong?
المحلول
Sinon fakeserver returns a 404 if the URL for which you have called it does not have a response assigned.
It appears that your problem is that the url you are calling is not the exact one in the respondWith() parameter. Also, there may be a limit on URL length in Sinon, not sure though.
نصائح أخرى
I am having a similar problem with. It seems to be related to the turning off of the cache in the AJAX call. I will post more if I get around it. You could try turning off the cache for the test and seeing if it passes. Not sure why it needs that though.
Ronan