javascript-无法从sinon.js fakeServer中触发对$.ajax的调用
作者:互联网
我有以下茉莉花规格:
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)
addReviewStatus是我编写的jQuery插件:
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.
上面的getJSON方法像这样调用$.ajax:
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
});
}
不会触发getJSON方法的匿名函数回调.同样,对$.ajax的调用返回未找到的404.谁能看到我在做什么错?
解决方法:
如果您为其调用的URL没有分配响应,则Sinon fakeserver返回404.
看来您的问题是您正在调用的url与responseWith()参数中的URL不完全相同.此外,虽然不确定,但Sinon中的URL长度可能有限制.
标签:ajax,jasmine,sinon,javascript 来源: https://codeday.me/bug/20191101/1985350.html