【问题标题】:loop through it() in jasmine describe output spec not found在茉莉花中循环遍历它()描述未找到输出规范
【发布时间】:2015-08-24 14:17:55
【问题描述】:

我了解到this way 是在 describe() 中循环遍历 it() 的最佳方式,但它因“未找到规范”而失败,并且似乎在 for 循环函数之前停止,我想知道我哪里做错了?

谢谢!

describe('this is my looping test!', function() {
  var input = [1,2,3];
  var output = [10, 20, 30];

  function test_my_times_ten(input, output) {
    it('should multiply ' + input + ' by 10 to give ' + output, function() {
      expect(input * 10).toEqual(output)
    });
  }

  for(var x = 0; x < input.size; x++) {
    test_my_times_ten(input[x], output[x]);
  }
});

【问题讨论】:

  • 您是否尝试过在循环之外使用它(“..”,cb)?有用吗?
  • @elad.chen 不确定这将如何工作....你能证明你的意思吗?非常感谢! :)

标签: javascript unit-testing jasmine jasmine-node


【解决方案1】:

其实有人做了这么聪明的事情,而且看起来确实有效!

Looping on a protractor test with parameters

var testParams = testConfig.testArray;

for (var i = 0; i < testParams.length; i++) {

  (function (testSpec) {
    it('write your test here', function () {
      //test code here
    });
  })(testParams[i]);

};

【讨论】:

  • 虽然我不喜欢循环测试,但这实际上是正确的方法。
  • @MBielski 哼...为什么不呢?大多数人不是需要测试多种类型的数据集,看看模型能不能接受吗? (ps:我是单元测试的菜鸟)。如果不是,请解释为什么不,以及替代方案?将不胜感激!
  • 循环违反了每个测试一个断言 (OAPT) 的原则。我们使用 OAPT 来保持测试的清晰性,无论是在编写测​​试还是读取测试。有很多链接可以找到争论一种或另一种方式。我坚信 OAPT。
  • @MBielski 酷!我可以知道在 OAPT 下,使用 jasmine 测试特定测试变量输入的多组值的最佳方法是什么?真的很感激!这是我不知道的事情!
  • 你需要看看你为什么要测试每个值。如果测试乘法函数是您的目标,则无需测试数组中的每个值。通过一项测试通过该功能就足够了。话虽如此,您还应该测试该函数对非数字输入的反应。期待它失败,看看你会得到什么。 FWIW,@Chen 正确回答了您的问题,您应该接受答案。
【解决方案2】:

我认为真正的问题是 "for(var x = 0; x

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 2014-12-12
    • 1970-01-01
    • 2018-01-13
    相关资源
    最近更新 更多