Suppose I have a function that returns a number multiplied by two.
function double(number){
return number * 2;
}
To test it with Mocha and Chai is easy.
var expect = require("chai").expect;
describe("#double", function(){
it("should double a number", function(){
expect(double(2)).to.equal(4);
expect(double(4.5)).to.equal(9);
expect(double(-3)).to.equal(-6);
});
});
But now suppose I have a function that returns a random integer between two specified integers, call them min
and max
.
function randBetween(min, max){
return Math.floor(Math.random() * (max - min + 1) + min);
}
I won't be able to predict what the returned value will be equal to. I could write a test that makes sure the returned value is both greater than or equal to min
and less than or equal to max
, but that doesn't actually test whether the returned value is random. Is there a statistically valid way for me to test whether my randBetween
function is working?
P.S. I know the function does not handle invalid arguments, e.g. if max
is less than min
. Please leave that aside for now.