javascript - Unit testing a function's parameter that is a function -


i know how write unit test parameter function in javascript.

for example,

_.foreach($scope.all, function(key, value) {    $scope.arr.push(key.name); }); 

i able enter foreach function in unit test, , able access first parameter, not able access function(key, value) {} param.

secondly, function parameter called? anonymous function?

in unit tests, shouldn't concerned testing every implementation detail. don't care anonymous function, rather side effects or return value of sut matches expectations. should test $scope.arr contains expected data after calling function.

if needed access function, you'd have use spy record when _.foreach called, access argument:

var foreachspy = sinon.spy(_, 'foreach'); yourfn(); var foreachfn = foreachspy.lastcall.args[1]; 

but again, argue testing in manner not add value , leads brittle tests not remain useful upon refactoring of implementation details. example, use regular array foreach in future; test break, don't care how loop through structure, scope correct.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -