Last active
October 5, 2016 16:51
-
-
Save froots/b479af461c06b647c72ad12402fd534a to your computer and use it in GitHub Desktop.
Stubbing Math.random()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function random(vmin, vmax) { | |
return [ | |
vmin[0] + Math.random() * (vmax[0] - vmin[0]), | |
vmin[1] + Math.random() * (vmax[1] - vmin[1]) | |
]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as V from './vector'; | |
import td from 'testdouble'; | |
it('returns a random vector within the bounds given', () => { | |
const rand = td.replace(Math, 'random'); | |
td.when(rand()).thenReturn(0.2, 0.8); | |
const vmin = [-50, -100]; | |
const vmax = [50, 0]; | |
const actual = V.random(vmin, vmax); | |
expect(actual).toEqual([-30, -20]); | |
td.reset(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment