【问题标题】:Callback not firing from the sinon.js fakeServer for a call to $.ajax未从 sinon.js fakeServer 触发回调以调用 $.ajax
【发布时间】:2012-03-10 20:13:14
【问题描述】:

我有以下茉莉花规格:

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 not found。谁能看到我做错了什么?

【问题讨论】:

    标签: javascript ajax jasmine sinon


    【解决方案1】:

    如果您调用它的 URL 没有分配响应,Sinon fakeserver 将返回 404。

    看来您的问题是您调用的 url 不是 respondWith() 参数中的 exact 之一。另外,Sinon 中的 URL 长度可能有限制,但不确定。

    【讨论】:

      【解决方案2】:

      我也有类似的问题。这似乎与AJAX调用中关闭缓存有关。如果我能解决它,我会发布更多。您可以尝试关闭缓存以进行测试并查看它是否通过。不知道为什么它需要它。

      罗南

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-24
        • 2011-09-30
        相关资源
        最近更新 更多