javascript - Jasmine 中 NUnit 的 TestCaseSource 是否有相应的设置?

标签 javascript unit-testing jasmine nunit

在使用 NUnit 编写单元测试时,您可以使用 TestCaseSourceAttribute 提供多个数据输入组合。来自 NUnit's documentation 的示例:

private static object[] DivideCases = {
    new object[] {12, 3, 4},
    new object[] {12, 2, 6},
    new object[] {12, 4, 3}
};

[Test, TestCaseSource("DivideCases")]
public void DivideTest(int n, int d, int q) {
    Assert.AreEqual(q, n/d);
}

这将使用 DivideCases 字段提供的参数运行 DivideTest 三次。

有没有办法用 Jasmine 实现类似的设置?

最佳答案

我知道这是一个老问题,但这种方法对我有用。

describe("divideTest", function () {
  const testCases = [
    { n: 12, d: 3, q: 4 },
    { n: 12, d: 2, q: 6 },
    { n: 12, d: 4, q: 5 }
  ];

  testCases.forEach(test => {
    it(`should divide ${test.n} by ${test.d} correctly`, () => {
      expect(test.n / test.d).toEqual(test.q);
    });
  });
});

我在这里找到了解决方案 https://blog.harveydelaney.com/running-multiple-test-cases-in-jasmine/

关于javascript - Jasmine 中 NUnit 的 TestCaseSource 是否有相应的设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30292162/

相关文章:

unit-testing - 如何在 Squeak 中自动化单元测试

javascript - Jest spyOn 不是类或对象类型的函数

angularjs - 让 Karma/Jasmine 为 AngularJS 项目工作

javascript - 第三方API跨域请求(https)

javascript - 在 javascript 中检查内容 css

python - 为什么要这样枚举呢?

objective-c - iOS 单元测试 : Can't link to symbols in my Application target. 怎么了?

javascript - .Remove() 删除所有 div 元素

javascript - JS : Return result from nested ajax success function

javascript - indexedDB.deleteDatabase() 抛出错误