I have a short function which gets an array and shuffles the values "around".
array_name.sort(function() { return 0.5 - Math.random() });
How can I test that I get a different array then I had before?
Of course I can mock the array, and then test if
array_name[2] != array_after[2];
But, as its random, it could happen that sometimes this position is equal and sometimes not.
What is a good approach to this?
So, its about testing the randomness
I have an approach, and of course my problem:
it('different option order when randomization is true', function() {
var array = // getting from json.file here
var array_after = question.shuffle(array);
expect(array[4].text.localeCompare(array_after[1].text)).toBe(0);
});
Of course, i cannot say now that this is always different. Because i run my tests several times, and sometimes they are the same, and sometimes not...