1 var assert = require('assert');
3 var mustCallChecks = [];
5 function runCallChecks() {
6 var failed_count = 0;
7 for (var i=0 ; i< mustCallChecks.length; ++i) {
8 var context = mustCallChecks[i];
9 if (context.actual === context.expected) {
10 continue;
11 }
13 failed_count++;
14 console.log('Mismatched %s function calls. Expected %d, actual %d.',
15 context.name,
16 context.expected,
17 context.actual);
18 console.log(context.stack.split('\n').slice(2).join('\n'));
19 }
21 assert(failed_count === 0);
22 }
24 after(runCallChecks);
26 exports.mustCall = function(fn, expected) {
27 if (typeof expected !== 'number') expected = 1;
29 var context = {
30 expected: expected,
31 actual: 0,
32 stack: (new Error).stack,
33 name: fn.name || '<anonymous>'
34 };
36 mustCallChecks.push(context);
38 return function() {
39 context.actual++;
40 return fn.apply(this, arguments);
41 };
42 };