摩卡,柴 - 循环测试

时间:2017-05-22 06:55:52

标签: javascript unit-testing mocha chai

我应该如何编写应该测试的函数的单元测试:50 < n < 100?测试中有比循环更好的方法吗?我可以将参数传递给测试函数以根据参数生成测试用例吗?离。

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

它应该将结果显示为3种不同的测试:

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

https://plnkr.co/edit/HBJP46lyTbBkH4Pclu2S

1 个答案:

答案 0 :(得分:4)

如果您正在使用this tutorial

[ 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);
  })
});