lib/qmock.js
/**
 * Decorate the tester with methods getMock, getMockSkipConstructor, and
 * arrange for the mock expectations to be checked when the tests are done.
 */
Decorate the tester with methods getMock, getMockSkipConstructor, and
arrange for the mock expectations to be checked when the tests are done.
Decorate the tester with methods getMock getMockSkipConstructor and arrange for the mock expectations to be checked when the tests are done .
function extendWithMocks( tester, doneMethod ) {
    if (tester.__qmockList) {
        throw new Error("unable to provide mocks, __qmockList already exists");
    }
    tester.__qmockList = [];
    tester.getMock = function getMock(constructor, methods, constructorArgs) {
        var mock = QMock.getMock(constructor, methods, constructorArgs);
        tester.__qmockList.push(mock);
        return mock;
    };
    tester.getMockSkipConstructor = function getMockSkipConstructor(constructor, methods) {
        var mock = QMock.getMockSkipConstructor(constructor, methods);
        tester.__qmockList.push(mock);
        return mock;
    };
    for (var name in module.exports) {
        if (name !== 'QMock' && name[0] !== '_' && !tester[name]) {
            tester[name] = module.exports[name];
        }
    }
    function tryCheck( mock ) {
        try { QMock.check(mock); QMock.reset(mock); }
        catch (err) { return err; }
    }
    if (!doneMethod) doneMethod = 'done';
    var oldDoneMethod = tester[doneMethod];
    tester[doneMethod] = function() {
        var i, err;
        for (i=0; i<tester.__qmockList.length; i++) {
            var e = tryCheck(tester.__qmockList[i]);
            err = err || e;
        }
        tester.__qmockList = [];
        if (oldDoneMethod) oldDoneMethod.apply(tester, arguments);
        if (err) throw err;
    };
    return tester;
}
function extendWithMocks ( tester , doneMethod ) { if ( tester . __qmockList ) { throw new Error ( "unable to provide mocks, __qmockList already exists" ) ; } tester . __qmockList = [ ] ; tester . getMock = function getMock ( constructor , methods , constructorArgs ) { var mock = QMock . getMock ( constructor , methods , constructorArgs ) ; tester . __qmockList . push ( mock ) ; return mock ; } ; tester . getMockSkipConstructor = function getMockSkipConstructor ( constructor , methods ) { var mock = QMock . getMockSkipConstructor ( constructor , methods ) ; tester . __qmockList . push ( mock ) ; return mock ; } ; for ( var name in module . exports ) { if ( name !== 'QMock' && name [ 0 ] !== '_' && ! tester [ name ] ) { tester [ name ] = module . exports [ name ] ; } } function tryCheck ( mock ) { try { QMock . check ( mock ) ; QMock . reset ( mock ) ; } catch ( err ) { return err ; } } if ( ! doneMethod ) doneMethod = 'done' ; var oldDoneMethod = tester [ doneMethod ] ; tester [ doneMethod ] = function ( ) { var i , err ; for ( i = 0 ; i < tester . __qmockList . length ; i ++ ) { var e = tryCheck ( tester . __qmockList [ i ] ) ; err = err || e ; } tester . __qmockList = [ ] ; if ( oldDoneMethod ) oldDoneMethod . apply ( tester , arguments ) ; if ( err ) throw err ; } ; return tester ; }
Decorate the tester with methods getMock, getMockSkipConstructor, and
 arrange for the mock expectations to be checked when the tests are done.
Decorate the tester with methods getMock getMockSkipConstructor and arrange for the mock expectations to be checked when the tests are done .
// decorate with all other exported methods too, // eg stub, spy, mockTimers, mockHttp // catch the assertion errors, finish the test, then re-throw the first. // This will have nodeunit avoid a silly "undone tests" error //QMock.check(tester.__qmockList[i]); //QMock.reset(tester.__qmockList[i]); // empty the qmock list, in case the tester runs multiple tests (eg mocha) // call the 'done' method before throwing the error to avoid "undone tests"