thlorenz / proxyquireify

browserify >= v2 version of proxyquire. Mocks out browserify's require to allow stubbing out dependencies while testing.
MIT License
151 stars 24 forks source link

How to stub object's property, not method? #47

Closed prh7 closed 8 years ago

prh7 commented 8 years ago

Though this might not be an issue but you can take it simply as question. The question is: I have a file config.js with the below code in it:

module.exports:
{
   development: {
        switch: false
        }
}

I have another file bus.js with the below code in it:

var config=require('config.js');

getBusiness:function(req,callback){
         if(config.switch) {
                  // Do something
         }else{
                 // Do something else
         }
}

Now, I want to unit test the file bus.js

require('mocha');

var chai = require('chai'),
      expect = chai.expect,
      proxyquire = require('proxyquire');

var bus = proxyquire('bus.js', {
                 'config':{
                        switch:true
                  }
});

describe("Unit Test', function() {

        it('should stub the config.switch', function(done) {
            bus.getBusiness(req, function(data) {
              // It should stub the config.switch with true not false and give code coverage for if-else statmt. 
            });
           done();
        });
});

Any sort of suggestion or guidance will be highly appreciated.

bendrucker commented 8 years ago

'config' is wrong. If you use correct paths ('./config.js') you can just as easily proxyquire non-function values as you can methods.

prh7 commented 8 years ago

@bendrucker Ok I have used the correct path. How do you stub object's proprerty? The way I have done gives me null or undefined when i console.log(config.switch) inside test case.

bendrucker commented 8 years ago

Please post a full project I can run, hard to guess at what's wrong otherwise