Skip to content

Instantly share code, notes, and snippets.

@bengl
Created April 1, 2014 18:17
Show Gist options
  • Select an option

  • Save bengl/9919844 to your computer and use it in GitHub Desktop.

Select an option

Save bengl/9919844 to your computer and use it in GitHub Desktop.
randomized mock data tests
$ mocha test.js
0 passing (4ms)
1 failing
1) RandomStuff should work for all of mockData:
Error: 2/6 failed
at Context.<anonymous> (/Users/benglish/testcrap.js:26:19)
at Test.Runnable.run (/Users/benglish/nodes/0.10.23/lib/node_modules/mocha/lib/runnable.js:221:32)
at Runner.runTest (/Users/benglish/nodes/0.10.23/lib/node_modules/mocha/lib/runner.js:374:10)
at /Users/benglish/nodes/0.10.23/lib/node_modules/mocha/lib/runner.js:452:12
at next (/Users/benglish/nodes/0.10.23/lib/node_modules/mocha/lib/runner.js:299:14)
at /Users/benglish/nodes/0.10.23/lib/node_modules/mocha/lib/runner.js:309:7
at next (/Users/benglish/nodes/0.10.23/lib/node_modules/mocha/lib/runner.js:247:23)
at Object._onImmediate (/Users/benglish/nodes/0.10.23/lib/node_modules/mocha/lib/runner.js:276:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)
$
describe('RandomStuff', function(){
var mockData;
before(function(){
mockData = [
{name: 'bob'},
{name: 'bob'},
{name: 'bob'},
{name: 'bill'},
{name: 'bill'},
{name: 'bob'},
];
});
it('should work for all of mockData', function(){
var errors = [];
shuffleArray(mockData).forEach(function(datum){
try {
// do some test on the datum, make some assertions
console.assert(datum.name === 'bob');
} catch (err) {
errors.push(err);
}
});
if(errors.length > 0) {
throw new Error(errors.length+'/'+mockData.length+' failed');
}
});
});
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment