对$.ajax的调用没有从sinon.js fakeServer启动回调
Callback not firing from the sinon.js fakeServer for a call to $.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不是responsdWith()参数中的精确的url。此外,Sinon可能对URL长度有限制,但不确定。
我遇到了类似的问题。这似乎与AJAX调用中关闭缓存有关。如果我能绕过它,我会发布更多。你可以试着关闭测试的缓存,看看它是否通过。但不确定它为什么需要这个。
Ronan
相关文章:
- node.js测试事件是否是在不使用超时的情况下使用sinon.js发出的
- 什么'与Jasmine相比,使用Sinon.js的优势是什么;内置于间谍
- 是否可以使用Sinon.JS检查函数参数绑定是否正确
- sinon.js验证mock方法's带回调的参数
- 使用sinon.js创建一个“;“间谍对象”;使用基于真实构造函数/原型的间谍方法
- stub.callsArg(index) from Sinon.JS 是做什么的
- Stubbing async.waterfall with Sinon.JS
- 如何模拟快乐.js回复 Sinon 进行单元测试
- 测试Express.js res.render在承诺与Mocha&Sinon spy
- Sinon Not Mocking db-mysql Node.js library
- 使用 Sinon.js 测试 navigator.browserLanguage 或 navigator.languag
- 如何使用 sinon.js 来模拟/监视 javascript 函数中提到的对象
- 使用 Sinon.js 测试一个函数是否调用 ES6 模块中的另一个函数
- sinon.js监视$.dataTables函数
- 如何在Jasmine中每次测试后自动恢复所有sinon.js间谍
- 如何使用nightwatch.js和sinon.js模拟API响应
- 如何使用sinon.js实现mock/stub回调函数
- Sinon JS:有没有一种方法可以在对象参数's在sinon js中的键值
- 测试Backbone.js模型保存使用Sinon不调用成功回调
- 有buster.js / sinon类似' jasmine.any() '的东西