Samman Thapa Samman Thapa - 4 months ago 39
Node.js Question

Using SinonJS stub (with rewire)

I have a function:

var publish = function(a, b, c) {
main = a + getWriterName(b,c);
}


and getWriterName is another function:

var getWriterName = function(b,c) {
return 'Hello World';
}


I want to test the "publish" function but I do not want to run the "getWriterName" function while I am testing "publish". I feel like I stub getWriterName function because I don't want to run it everytime I test "publish", but how do I do that? I did something like:

var sandbox = sinon.sandbox.create();
sandbox.stub(getWriterName).returns('done');


But this gives me an error of


TypeError: Attempted to wrap undefined property undefined as function


What is wrong with my stubbing if I am in the write path?

Edit:
I am using rewire so would like solutions using rewire

Answer

This solved my problem:

If my functions are in a file called main.js then firstly I'd rewire the file as:

var main = rewire('main');

Then to stub any other function being called in one function, in my case, when I had to stub getWriterName I'd do:

main.__set__('getWriterName', function(b, c) {
    return 'Something Else'
}

and Finally after finishing using it, do

main.restore();
Comments