4

I learned that this way is the best way to loop through the it() in a describe(), but it failed on me with "spec not found", and seems stop right before the for loop function, I wonder where did I do wrong?

Thanks!

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]);
  }
});
ey dee ey em
  • 7,991
  • 14
  • 65
  • 121

2 Answers2

7

Actually someone did really smart thing like this, and it seems does the work!

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]);

};
Community
  • 1
  • 1
ey dee ey em
  • 7,991
  • 14
  • 65
  • 121
  • While I'm not a fan of looping in tests, this is actually the correct way to do it. – MBielski Aug 24 '15 at 16:37
  • @MBielski hum... why not though? Doesn't most people need to test multiple type of data sets to see if the model can accept them? (ps: I am a noobie in unit-testing). If not, please explain why not, and alternative? Will be really appreciated! – ey dee ey em Aug 24 '15 at 16:46
  • Looping violates the principle of One Assertion Per Test (OAPT.) We use OAPT to maintain clarity in tests, both in writing them and in reading them. There are plenty of links to be found arguing one way or the other. I firmly believe in OAPT. – MBielski Aug 24 '15 at 17:03
  • 1
    @MBielski Cool! may I know under OAPT, what is the best way to use jasmine to test multiple sets of value for specific testing variable input? Will really appreciated! This is something I have no idea of! – ey dee ey em Aug 24 '15 at 17:30
  • You need to look at why you are testing each value. If testing the multiplication function is your goal, there is no need to test each value in the array. Passing that function through one test would suffice. That being said, you should also test how the function reacts to non-numeric input. Expect it to fail and see what you get. FWIW, @Chen answered your question correctly and you should accept the answer. – MBielski Aug 24 '15 at 19:41
  • @MBielski I know that it seems pretty clear with such as simple case as it is multiplication function. But in the real world testing with datasets is a must since there can be lots of combinations, especially if you're doing integration tests. It's fine to keep in mind OAPT when possible, but sometimes is not. At least, in my opinion. – José Manuel Blasco Apr 09 '20 at 10:38
1

i think the real problem is "input.size" at line "for(var x = 0; x < input.size; x++) {" . there is no such thing called "input.size". try input.length and your test will run as expected

cppython
  • 1,209
  • 3
  • 20
  • 30