3

我有以下茉莉花规格:

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

4

2 回答 2

2

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

于 2012-03-22T14:19:25.317 回答
2

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

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

于 2014-03-07T01:26:24.940 回答