2012-06-06 15 views
0

我嘗試使用QUnit來測試我的javascript代碼。 我有簡單的功能:如何在我的js測試中使用模擬?

function Multiply(a, b) { 
    return a * b; 

}

function CalculateBodyMassIndex(weight, height) { 
    return Math.round(weight/Multiply(height, height)); 

}

function SummAll(array) { 
    var summ = 0; 
    $.each(array, function (i, el) { 
     summ = summ + el; 
    }); 
    return summ; 

}

我有兩個問題: 1)我怎麼能確認的功能CalculateBodyMassIndex將被稱爲乘法函數?

2)如何驗證函數SummAll將被稱爲$ .each從jQuery庫?

謝謝你等待答案。

回答

-1

這是最簡單的利用功能的範圍界定,並允許您mockable功能可選參數:

function CalculateBodyMassIndex(weight, height, using) { 
    return Math.round(weight/(using || Multiply)(height, height)); 
} 

然後在您的測試:

var MockMultiplyRan = false; 
var MockMultiply = function(a,b) { 
    MockMultiplyRan = true; 
    return Multiply(a,b); 
} 

CalculateBodyMassIndex(200,200, MockMultiply); // arbitrary figures 
ok(MockMultiplyRan); 
+0

嗨。感謝你的回答。我在這個答案中感到尷尬。我必須在我的代碼中更改所有名爲CalculateBodyMassIndex的,不是嗎?結果我混合了工作邏輯和測試邏輯。我認爲這是錯誤的,因爲它是堅實的原則。你能解釋我這一刻嗎? – Telary

2

這是如何的優秀崗位用qunit將sinon.js用於mocks http://cjohansen.no/en/javascript/using_sinon_js_with_qunit

sinon中的間諜和存根允許您非常容易地驗證對現有對象的調用。

編輯 的sinon.js文檔這裏http://sinonjs.org/docs/#spies展示瞭如何使用間諜。瀏覽完整的API文檔,瞭解存根,模擬等示例。

+0

嗨。感謝您的答覆和鏈接。我有第二個問題。如果我想確保使用特定參數調用函數,我應該如何編寫測試?不幸的是我找不到文章中的例子。 – Telary

0

只需粘貼以下html並從瀏覽器中打開它。

<html> 
    <head> 
    <title>test</title> 
    <link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.11.0.css"> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
    <script src="http://code.jquery.com/qunit/qunit-1.11.0.js"></script> 
    <script src="http://sinonjs.org/releases/sinon-1.6.0.js"></script> 
    <script src="http://sinonjs.org/releases/sinon-qunit-1.0.0.js"></script> 
    <script type="text/javascript"> 
     function Multiply(a, b) { 
     return a * b; 
     } 

     function CalculateBodyMassIndex(weight, height) { 
     return Math.round(weight/Multiply(height, height)); 
     } 

     function SummAll(array) { 
     var summ = 0; 
     $.each(array, function (i, el) { 
      summ = summ + el; 
     }); 
     return summ; 
     } 
    </script> 
    </head> 
    <body> 
    <div id="qunit"></div> 
    <script type="text/javascript"> 
     $(function(){ 
     test('Multiply', function(){ 
      $.each([ 
      [[0, 2], 0], 
      [[3, 3], 9], 
      [[7, 9], 63] 
      ], function(){ 
      var t = this.toString(); 
      equal(
       Multiply.apply(this, this[0]), this[1], 
       t + ': Multiply ok' 
      ); 
      }); 
     }); 

     test('CalculateBodyMassIndex', function(){ 
      this.spy(window, 'Multiply'); 
      $.each([ 
      [[1, 2], 0], 
      [[10, 2], 3], 
      [[35, 3], 4] 
      ], function(i){ 
      var t = this.toString(); 
      equal(
       CalculateBodyMassIndex.apply(this, this[0]), this[1], 
       t + ': CalculateBodyMassIndex ok' 
      ); 
      ok(Multiply.calledOnce, t + ': call Multiply ok'); 
      deepEqual(
       Multiply.args[0], [this[0][1], this[0][1]], 
       t + ': Multiply args ok' 
      ); 
      Multiply.reset(); 
      }); 
     }); 

     test('SummAll', function(){ 
      $.each([ 
      [[1, 2, 3, 4, 5], 15], 
      [[2, 3, 4, 5, 6], 20], 
      [[3, 4, 5, 6, 7], 25] 
      ], function(){ 
      var t = this.toString(); 
      equal(
       SummAll(this[0]), this[1], 
       t + ': SummAll ok' 
      ); 
      }); 
     }); 
     }); 
    </script> 
    </body> 
</html> 
相關問題