I am trying to write a jasmine unit test for my sorted programme.. i am new in writing jasmine as well as test case.. providing my code an-below below... can you guys tell me how to do it in a fiddle...
var myNumbersToSort = [-1, 2, -3, 4, 0.3, -0.001];
function getClosestToZero(numberSet) {
var i = 0, positiveSet = [], positiveClosest = 0;
for (i = 0; i < numberSet.length; i += 1) {
positiveSet.push(numberSet[i] >= 0 ? numberSet[i] : numberSet[i] * -1);
}
positiveClosest = Math.min.apply(Math, positiveSet);
return numberSet[positiveSet.indexOf(positiveClosest)];
}
alert(getClosestToZero(myNumbersToSort));
Example test cases could look like this
You can think of more test cases. Test any positive and negative behavior and try to think of edge cases. Keep in mind that tests are not just to prove your code is currently working, but also to ensure that it doesn't break during future development.
Possible edge cases:
undefined
values in the array (orNaN
,Infinity
, …)-0.01
and0.01
)