tdd – 如何重复/循环mocha测试

前端之家收集整理的这篇文章主要介绍了tdd – 如何重复/循环mocha测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在做一些mocha / chai测试,除了在每个’it’测试中放置一个循环并迭代一次和一次之外,我还没有找到一种很好的方法来运行我的测试而不是许多不同的可能性.问题是,如果我有数十或数百个测试,我不想一遍又一遍地写相同的for循环.

这样做有更优雅的方式吗?特别是一个用不同的测试参数一次遍历所有测试的?

describe('As a dealer,I determine how many cards have been dealt from the deck based on',function(){

  console.log(this);

  beforeEach(function(){
    var deck = new Deck();
    var myDeck = deck.getCards();
  });


    it('the number of cards are left in the deck',function(){
      for(var i = 1; i<=52; i++){
        myDeck.dealCard();
        expect(myDeck.countDeck()).to.equal(52-i);
      }
    });

    it('the number of cards dealt from the deck',function(){
      expect(myDeck.countDealt()).to.equal(i);
    });

    it('the sum of the cards dealt and the cards left in the deck',function(){
      expect(myDeck.countDeck() + myDeck.countDealt()).to.equal(52)
    });

});
我在 Loop Mocha tests?实现了neezer的解决方案,它涉及将整个测试放入一个闭包并用循环执行它.

请注意,循环在函数中使用beforeEach(),因为它每次测试执行52次.如果这些元素是动态的,并且每个循环不会执行多次,那么将元素放在beforeEach()函数中并不是一个好主意.

代码看起来像这样,它似乎工作.

var myDeck = new Deck(Card);

function _Fn(val){

    describe('As a dealer,function(){

      myDeck.dealCard();

      var cardCount = 0;
      var dealtCount = 0;

      cardCount = myDeck.countDeck();
      dealtCount = myDeck.countDealt();

      it('the number of cards are left in the deck',function(){
        expect(cardCount).to.equal(52-val);
      });

      it('the number of cards dealt from the deck',function(){
        expect(dealtCount).to.equal(val);
      });

      it('the sum of the cards dealt and the cards left in the deck',function(){
        expect(cardCount + dealtCount).to.equal(52);
      });

    });

}

for(var i = 1; i<=52; i++){
  _Fn(i);
}
原文链接:https://www.f2er.com/javaschema/281671.html

猜你在找的设计模式相关文章