Skip to content

Instantly share code, notes, and snippets.

@froots
Last active October 5, 2016 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save froots/b479af461c06b647c72ad12402fd534a to your computer and use it in GitHub Desktop.
Save froots/b479af461c06b647c72ad12402fd534a to your computer and use it in GitHub Desktop.
Stubbing Math.random()
export function random(vmin, vmax) {
return [
vmin[0] + Math.random() * (vmax[0] - vmin[0]),
vmin[1] + Math.random() * (vmax[1] - vmin[1])
];
}
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