如何在茉莉花中描述it()内部的测试用例

时间:2019-01-21 23:03:45

标签: jasmine

到目前为止,这是我的代码:

import diceroll, {maxVal, minVal} from './index';

let testset=[
  // definition, min, max
  ["1",1,1],
  ["w6",1,6],
  ["1w6",1,6],
  ["2w6",2,12],
  ["2w6+12",14,24],
  ["2w6+12+2w3",16,30],
  ["3w6-3",0,15],
];

describe('lib/diceroll', () => {
  it('should parse correctly', () => {
    for (let i = 0; i < testset.length; i++) {
      let definition = testset[i][0];
      let minToBe = testset[i][1];
      let maxToBe = testset[i][2];

      let min = minVal(definition);
      let max = maxVal(definition);

      // @todo: OnFailure tell me the current definition!

      expect(minVal(definition)).toBe(minToBe);
      expect(maxVal(definition)).toBe(maxToBe);

      for (let n = 0; n < 100; n++) {
        let r = diceroll(definition);
        expect(r).toBeLessThanOrEqual(minToBe);
        expect(r).toBeGreaterThanOrEqual(maxToBe);
      }
    }
  });
});

我的问题:如果某些期望失败,我不知道哪个骰子定义失败。我试图在it()中调用describe()-不允许。

这里的最佳做法是什么?在我的测试集循环中使用it()?还是我完全在这里做某事?

1 个答案:

答案 0 :(得分:1)

茉莉花匹配器有一个可选的第二个参数:

  

(method) jasmine.Matchers<number>.toBeLessThanOrEqual(expected: number, expectationFailOutput?: any): boolean

因此您可以编写类似以下内容以输出发生故障的位置:

for (let n = 0; n < 100; n++) {
  let r = diceroll(definition);
  expect(r).toBeLessThanOrEqual(minToBe, 'failed when i='+i+' n='+n);
  expect(r).toBeGreaterThanOrEqual(maxToBe, 'failed when i='+i+' n='+n);
}