【问题标题】:Is there a corresponding setup for NUnit's TestCaseSource in Jasmine?Jasmine 中的 NUnit 的 TestCaseSource 是否有相应的设置?
【发布时间】:2015-05-17 21:06:54
【问题描述】:

在使用 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 实现类似的设置?

【问题讨论】:

    标签: javascript unit-testing jasmine nunit


    【解决方案1】:

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

    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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多