简体   繁体   中英

How do I spyOn private functions in a node module using jasmine-node?

app.js

function _private() {
    console.log( '_private' );
}

function public() {
    console.log( 'public' );
    _private();
}

module.exports = {
    public: public,
    _private: _private
};

spec/appSpec.js

describe( 'test', function() {
    it( 'will spy on _private', function() {
        var app = require( '../app' );
        spyOn( app, '_private' );
        app.public();
        expect( app._private ).toHaveBeenCalled();
    });
});

_private() is called, but the spy doesn't work and the test fails.

So as the question asks, how do I hook the spy up so that it knows that _private() was called? Or is this not possible?

You can call _private with this otherwise the function is not defined. try this:

function public() {
    console.log( 'public' );
    this._private();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM