Samman Thapa Samman Thapa - 1 year ago 115
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();

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?

I am using rewire so would like solutions using rewire

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download