最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mocha, Chai - loop in test - Stack Overflow

programmeradmin1浏览0评论

How should I write unit test for function which should test: 50 < n < 100? Is there better way than loop in test? Can I pass arguments to test function to generate test case based on that arguments? ex.

it('should return $n+1', function(n){
  expect(add(n)).to.be.eql(n+1);
},[ 1, 2, 3])

and it should show result as 3 different tests:

should return 1+1
should return 2+1
should return 3+1

How should I write unit test for function which should test: 50 < n < 100? Is there better way than loop in test? Can I pass arguments to test function to generate test case based on that arguments? ex.

it('should return $n+1', function(n){
  expect(add(n)).to.be.eql(n+1);
},[ 1, 2, 3])

and it should show result as 3 different tests:

should return 1+1
should return 2+1
should return 3+1

https://plnkr.co/edit/HBJP46lyTbBkH4Pclu2S

Share Improve this question edited May 22, 2017 at 7:11 Alcadur asked May 22, 2017 at 6:55 AlcadurAlcadur 6851 gold badge8 silver badges24 bronze badges 1
  • That sounds like wishful thinking if Chai doesn't work that way. – tadman Commented May 22, 2017 at 7:00
Add a ment  | 

1 Answer 1

Reset to default 8

If you're using Mocha:

[ 1, 2, 3 ].forEach(value => {
  it(`should return ${ value }+1`, () => {
    expect(add(value)).to.be.eql(value + 1);
  })
});

// The same, but for older versions of Node.js/browser:
[ 1, 2, 3 ].forEach(function(value) {
  it('should return ' + value + '+1', function() {
    expect(add(value)).to.be.eql(value + 1);
  })
});
发布评论

评论列表(0)

  1. 暂无评论