2013-07-09 19 views
2

我無法窺探單個測試規範中的多個ajax請求。我想使用JASMINE測試以下myFirstFunction()mySecondFunction()如何使用JASMINE監聽多個AJAX呼叫?

it("Test myFirstFunction & mySecondFunction", function() { 
     var operation = "customerPreviousOrder"; 
     var myFirstFunctionData = [{"accessFlag":true}] 
     spyOn($, "ajax").andCallFake(function(params) { 
      params.success(myFirstFunctionData); 
     }); 
     myFirstFunction(); 
     expect(<Something>).toEqual(<Something>); 

}); 

我想從上面的測試規範測試mySecondFunction() -

function myFirstFunction() { 
    $.postJSON('test1.htm', {operation:operation}, function (data) {  
     if (data != null && data.errorMsg != null && data.errorMsg.length > 0) { 
      $.populateErrorMessage(data); 
     } else { 
      operationAccess = data; 
      var tempAccessFlag = operationAccess[0].accessFlag; 
      if (tempAccessFlag) { 
       mySecondFunction(); 
      }     
     } 
    }); 
} 

function mySecondFunction(operation, operationAccess, reason) { 
     $.postJSON('test2.htm', {windowStart:0, windowSize:4}, function (data) { 
      if (data != null && data.errorMsg != null && data.errorMsg.length > 0) { 
       $.populateErrorMessage(data); 
      } else { 
       if (null != data && data.accessFlag == "SUCCESS") { 
        //do something 
       } else { 
        //do something 
       } 
      } 
     }); 
} 

我如下如下寫了下面的測試規範&能夠窺探第一postJSON。由於myFirstFunction()致電mySecondFunction()。那麼我怎麼能窺探第二個postJSON呢?

回答

1

你應該使用sinonJS它有一個fake server嘲笑阿賈克斯的東西,所以你不必關心它。

var server = sinon.fakeServer.create(); 
server.respondWith('test1.htm', '[{"accessFlag":true}]'); 
server.respondWith('test2.htm', 'second answer'); 
server.autoRespond = true; 

myFirstFunction(); 
0

你可以試試下面的問題的解決方案假人。

源功能:

function myFunction(){ 
$.ajax({        //First Ajax Call 
    url: <URL1>, 
    type: 'POST', 
    dataType: 'json', 
    data: JSON.stringify(data), 
    contentType: "application/json; charset=utf-8", 
    async: false, 

    success: function (data) { 
      … some success statement 
    } else { 
      var enableCardReaderCall = $.ajax({   //Nested Ajax Call 
        type: 'POST', 
        url: <URL2>, 
        dataType: 'json', 
     data: JSON.stringify(data), 
        contentType: "application/json; charset=utf-8", 

        success: function (result) { 
    // … some success statement 
        } 
       } 
}); 

}); }

測試套件:

describe("Test Suit", function(){ 
    beforeEach(function() { 
     var fakeData = "hi"; 
    spyOn($, "ajax").andCallFake(function(params) {   //First Spy On 
     params.success(function(fakeData){ 
      spyOn($, "ajax").andCallFake(function(params) {  //Nested Spy On 
       params.success("there is data") 
      }) 
     }); 
    }); 
}); 
it("Test Function", function() { 
    $.myFunction(); 
}); 

});

0

ajax函數的參數具有url值。

describe("Test Suit", function() { 
    beforeEach(function() { 
    spyOn($, 'ajax').and.callFake(function(options) { 
     if (options.url === 'test1.htm') { 
     options.success([{"accessFlag":true}]); 
     } else { 
     options.success('second answer'); 
     } 
    }); 
    }); 
    it("Test myFirstFunction & mySecondFunction", function() { 
    myFirstFunction(); 
    expect(<Something>).toEqual(<Something>); 
    } 
}); 
1

從樞紐開發和維護Jasmine-Ajax的傢伙,這增加了AJAX的支持,茉莉花,所以你可以測試請求和模擬響應。模擬是通過

jasmine.Ajax.useMock(); 

安裝在測試或beforeEach在測試中的所有請求都收集和全局變量ajaxRequests可用。沒有任何響應返回到這些請求中的任何一個。要模擬響應,您必須在測試中呼叫.response([some data])

因此,這將是這樣的:

it("Test myFirstFunction & mySecondFunction", function() { 
    var myFirstFunctionData = {responseText: '[{"accessFlag":true}]'}; 
    jasmine.Ajax.useMock(); 
    myFirstFunction(); 
    ajaxRequests[0].response(myFirstFunctionData); // index depends on the order of the AJAX calls 
    expect(<Something>).toEqual(<Something>); 

}); 

注:有全球性的方法以獲得最新的AJAX請求mostRecentAjaxRequest();。當你需要直接訪問ajaxRequests的內容請加:

afterEach(function(){ 
    clearAjaxRequests(); 
}); 
+0

的[茉莉花AJAX(https://github.com/jasmine/jasmine-ajax)你使用哪個版本?看起來像'jasmine.Ajax.useMock'沒有在2.0中定義:http://jasmine.github.io/2.0/ajax.html –

+0

我找不到版本號,但我添加了mock-ajax.js 2013年初。所以我想它是古老的:) – Sve

+0

從簡單的一下看:jasmine-ajax 3.2.0實現了一個requestTracker。請求通過'jasmine.Ajax.requests.at()'訪問。 – Sve