簡體   English   中英

在 Mocha/Chai for JS 上測試 40 多種組合

[英]Testing 40+ combinations on Mocha/Chai for JS

現在正在為 JS 學習 Mocha + Chai,我對如何測試這段代碼有點困惑:

for (var j = 12; j <= 19; j++) {
    if (cardNumber.length === j) {
        if (cardNumber.slice(0, 4) === '5018') {
            return 'Maestro';
        } else if (cardNumber.slice(0, 4) === '5020') {
            return 'Maestro';
        } else if (cardNumber.slice(0, 4) === '5038') {
            return 'Maestro';
        } else if (cardNumber.slice(0, 4) === '6304') {
            return 'Maestro';
        }
    }
}

我正在測試作為卡號的輸入字符串是否滿足以下任何條件,其中指定了前 4 個元素,並且卡的長度必須在 12 和 19 之間。

describe('Maestro', function() {
  // Write full test coverage for the Maestro card
  var should = chai.should();

  for (var length = 12; length <= 19; length++) {
    (function(length) {
      it ('has a prefix of 5018 and a length of ' + length, function() {
        detectNetwork('5018' + ???).should.equal('Maestro');
      });
    })(length)
  };
});

我不想為 4 個前綴中的每一個 + 12 - 19 之間的每個長度編寫所有測試用例。有沒有一種方法可以通過增加等於長度的元素數量來添加到我的前綴字符串中?

我試過在 for 循環中放置一個包含 12-19 的額外整數數組,並將其添加到測試函數的前綴中,但它仍然不起作用

我想你正在尋找這樣的東西:

describe('Maestro', function() {
  // Write full test coverage for the Maestro card
  var should = chai.should();
  var prefixes = ['5018', '5020', '5038', '6304'];
  for(var p=0; p<prefixes.length;p++) {
      for (var length = 12; length <= 19; length++) {
        (function(length, prefix) {
          it ('has a prefix of ' + prefix + ' and a length of ' + length, function() {
            var longString = 'abcdefghijklmnopqrstuvwxyz';
            var testString = prefix + longString.slice(0, length-prefix.length);
            detectNetwork(testString).should.equal('Maestro');
          });
        })(length, prefixes[p])
      };
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM