2014-09-02 44 views
0

我有以下四個函數,其中兩個(),三個()和四個()將在promise參數發生時被調用。讓我進一步解釋一下。當我調用函數one()時,我傳遞了默認參數值,但函數two()將在函數one()中用解析的promise值調用。函數two(),three()和four()的類似邏輯如下。如何做單元測試jQuery的承諾?

function one(arg) { 

    var deferred = $.Deferred(); // Don't worry yet what this is until after you understand the flow 

    console.log("Starting one's ajax with arg: " + arg); 
    $.ajax({ 
     url: '/', 
     success: function() { 

      // Here's where you want to call the next function in the 
      // list if there is one. To do it, call deferred.resolve() 
      console.log('Finished with one. Ready to call next.'); 
      deferred.resolve("This is one's result"); 

     } 

    }); 

    // The deferred object has a "promise" member, which has a "then" function 
    return deferred.promise(); 
} 

function two(arg) { 
    var deferred = $.Deferred(); 
    console.log("Starting two's ajax with arg: " + arg); 
    $.ajax({ 
     url: '/', 
     success: function() { 

      // Again, this is where you want to call the next function 
      // in the list if there is one. 
      console.log('Finished with two. Ready to call next.'); 
      deferred.resolve("This is two's result"); 

     } 

    }); 
    // The deferred object has a "promise" member, which has a "then" function 
    return deferred.promise(); 
} 

function three(arg) { 
    var deferred = $.Deferred(); 
    console.log("Starting three's ajax with arg: " + arg); 
    $.ajax({ 
     url: '/', 
     success: function() { 

      // Again, this is where you want to call the next function 
      // in the list if there is one. 
      console.log('Finished with three. Ready to call next if there is one.'); 
      deferred.resolve("This is three's result"); 

     } 

    }); 
    // The deferred object has a "promise" member, which has a "then" function 
    return deferred.promise(); 
} 

function four(arg) { 
    console.log("Starting four with arg: " + arg); 
    console.log("Finished synchronous four"); 
} 

// Test it out. Call the first. Pass the functions (without calling them, so no parentheses) 
// into the "then" calls. 

one("arg given to one") 
    .then(two) 
    .then(three) 
    .then(four); 
+0

你必須有一個延遲的反模式,$就已經返回了一個承諾 - 使用它。 – 2014-09-02 15:10:09

+0

另外,我可以理解你的代碼,但我不知道你在問這裏。 – 2014-09-02 15:10:37

+0

@BenjaminGruenbaum我想寫上述代碼的單元測試用例,以便稍後我可以應用類似的想法 – 2014-09-02 15:15:43

回答

1

目前,您測試的太多。你真的想測試你的瀏覽器能夠做AJAX嗎?做什麼的?

第一步是提取函數,以便您可以在單元測試中調用/鏈接它們。這樣,您可以在單元測試中創建自己的承諾,爲它們提供功能並同步執行它們。

0

在考慮了@Aaron的建議後,我嘗試了以下單元測試代碼,以獲取單個函數的成功場景,比如函數1。

describe('Test example-1', function() { 

    it('should check promise resolved with a custom message', function() { 
    spyOn($, 'ajax').andCallFake(function (req) { 
     var d = $.Deferred(); 
     d.resolve("I am done"); 
     return d.promise(); 
    }); 

    var expectedPromise = one(); 
    var result = "I am done"; 

    expectedPromise.then(function(response) { 
     expect(response).toEqual(result); 
    }); 
}); 
}); 

成功和失敗的例子我在GitHub庫推:

https://github.com/pulakb/UnitTesting-Promises/tree/master/Jasmine/jQuery