Monday, October 2, 2017

Function inside of for loop not being tested

Leave a Comment

I have a function im trying to test:

  vm.clearArray = function(){     for (var id=0; id<vm.copyArray.length;id++){       vm.styleIcon(vm.copyArray[id],'black')     }     vm.copyObjArray = [];     vm.copyArray = [];   } 

I'm trying to test it like:

it('should have cleared copyArray on function call', function(){      var ctrl = $componentController('copy', null);      spyOn(ctrl, 'clearArray').and.callThrough();     spyOn(ctrl, 'styleIcon').and.callThrough();      ctrl.copyArray = [123];     ctrl.clearArray();      expect(ctrl.clearArray).toHaveBeenCalled();     // expect(ctrl.styleIcon).toHaveBeenCalled();     expect(ctrl.copyObjArray).toEqual([]);     expect(ctrl.copyArray).toEqual([]);   }); 

If I uncomment the above expect I get an error and the vm.styleIcon call is never covered in my coverage report. By setting copyArray to contain a value in the array I would think that the for loop would then trigger when running the test. That does not seem to be the case.

Thanks.

2 Answers

Answers 1

The code of the loop looks good, so I think that the property vm.copyArray might not be set at all. If you add a console.log(vm.copyArray), what is the result?

Perhaps vm and $componentController('copy', null) are not references to the same object, but call each other's functions through some library? is there any other way to reference vm from the test script, rather than using $componentController('copy', null)?

Your loop must be triggered when you pass the array in the function as an argument. Of course your actually code will fail unless the pass vm.copyArray as an argument in the actual code, but it will show you if the loop is the problem, or the reference to the vm from the test script:

//tested function  vm.clearArray = function(copyArray){     for (var id=0; id<copyArray.length;id++){       vm.styleIcon(copyArray[id],'black')     }   }  //test ctrl.clearArray([123]); 

Answers 2

It's difficult to determine the exact reason because you've shown a sample taken straight from your test, and not a minimal, complete, and verifiable example. Also, you didn't specify what the error(s) or expect results are, so we're going off very limited information.

That said, I strongly suspect vm is undefined/null or not a prototype instantiatable through $componentController. If this is the case, you should be receiving an error at ctrl.clearArray();, never running the loop and thus never calling vm.styleIcon. In this scenario you'd need to verify that ctrl is in fact an instance of whatever prototype vm is a part of, and not a global variable.

If this is not the case and both the vm prototype is correct and $componentController('copy', null); is creating the object you think it is, perhaps styleIcon is undefined/null, unable to be called and creating essentially the same problem. In this scenario, ensure styleIcon is set and that it's the function you think it is.

When all else fails, debuggers are your friend.

Please specify what the error(s) are and where they're occuring(in more detail) for a better answer.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment