如何编写单元测试来嵌入ajax请求?

时间:2013-08-15 18:11:16

标签: qunit sinon mockjax

我有handleDownload方法,它启动文件下载。该函数发布到后端,后端根据新请求发布到文件所在的服务器返回响应。我看到我可以使用mockjax来模拟请求,但是如何处理不同的路径,如成功,错误等。我应该知道哪个响应触发哪条路径(成功,错误,完成,......) 。测试handleDownload函数会有什么好处,怎么样?对于嘲笑,我使用Sinon.js我还没有真正深刻的理解。我还应该检查是否调用了handleDownloadFinal函数。

handleDownload: function(data, url) {
$.ajax({
    type: "POST",
    url: url,
    data: {},
    success: function(response) {
        if (response.success) {
            var start_token = response.token;
            $.ajax({
                type: start_token.method,
                url: start_token.url,
                beforeSend: function(xhr) {
                    xhr.setRequestHeader('Authorization', start_token.header);
                },
                success: function(start_response) {
                    handleDownloadFinal(start_response.status_token);
                },
                error: function(start_response) {
                    $.ajax({
                        type: "POST",
                        url: url + 'proxy/',
                        success: function(fallback_response) {
                            if (fallback_response.success) {
                                handleDownloadFinal(fallback_response.status_token, true, fallback_response.job_uuid);
                            } else {
                                errorDownload(response.error);
                            }
                        },
                        error: function(fallback_response) {
                            // Now this is some real error                                    
                            generalErrorDownload();
                        },
                        dataType: 'json'
                    });
                },
                dataType: 'json',
                xhrFields: {
                    withCredentials: true
                }
            });
        } else {
            errorDownload(response.error);
        }
    },
    error: function(response) {
        generalErrorDownload();
    },
    complete: function() {
    },
    dataType: "json"
});

}

1 个答案:

答案 0 :(得分:2)

你应该使用来自sinon的fake server

before(function(){
    //create the server 
    this.server = sinon.fakeServer.create();
    // let the server automatically respond for every request
    server.autoRespond = true;
})

it('test something', function(){

    //let the server respond for  specific url with 200-ok
    this.server.respondWith("POST", "/some/article/comments.json", [200, {
      "Content-Type": "application/json"
    }, '[{ "id": 12, "comment": "Hey there" }]']);
})

由于你有很多请求,你必须检查所有组合,我建议为每个请求失败成功提供帮助函数,这样你就可以测试这样的情况:

function letFirstRequestSucceed() {
  this.server.respondWith("POST", "urlForFirstRequest", [200, {
    "Content-Type": "application/json"
  }, '[{ "id": 12, "comment": "Hey there" }]']);
}


function letSecondRequestFail() {
  this.server.respondWith("POST", "urlForSecondRequest", [404, {
    "Content-Type": "application/json"
  }, '{error: "some error message"}');
}


function letThirdRequestFail() {
  this.server.respondWith("POST", "urlForThirdRequest", [404, {
    "Content-Type": "application/json"
  }, '{error: "some error message"}');
}

it("should to something when the second and third request fails", function () {
  sinon.spy(window, 'generalErrorDownload');
  letFirstRequestSucceed();
  letSecondRequestFail();
  letThirdRequestFail();

  handleDownload('someDate', 'aUrl');

  expect(generalErrorDownload)

})

顺便说一句,您应该考虑使用api ajax调用支持的jquerys deferred重构代码,这样可以使您的内容更具可读性。