ä¹±æ°ã使ãé¢æ°ã®ãã¹ã
function coin(x) { return Math.random() < x; }
ããããé¢æ°ããã£ãã¨ãã¦ãã©ããã¹ããæ¸ãã®ãã
ã±ã£ã¨æãã¤ãã®ã¯ããããæãã§ãMath.random
èªä½ãä¸æ¸ãããããæ¹ã
describe("coin(x)", function() { it("works", sinon.test(function() { this.stub(Math, "random", function() { return 0.5; }); assert(coin(0.4) === false); assert(coin(0.5) === false); assert(coin(0.6) === true); })); });
ãã®ããæ¹ããããã«ééã£ã¦ããªããã ãã©ãé¢æ°ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹èªä½ãå¤æ´ããæ¹ãè¯ãã¨æãã
function coin(x, random) { random = random || Math.random; return random() < x; }
ããã ã¨ãã¹ããããããã ããããªããåã£ãä¹±æ°çæå¨ã¨ãã·ã¼ãä»ãã®ä¹±æ°çæå¨ã使ç¨ãããã§ãã¦ä¾¿å©ã100ä¸åã«ãªãã
describe("coin(x)", function() { it("works", function() { var random = function() { return 0.5; }; assert(coin(0.4, random) === false); assert(coin(0.5, random) === false); assert(coin(0.6, random) === true); }); });