对$.ajax的调用没有从sinon.js fakeServer启动回调

Callback not firing from the sinon.js fakeServer for a call to $.ajax

本文关键字:js sinon fakeServer 启动 回调 ajax 调用      更新时间:2023-09-26

我有以下茉莉花规格:

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