2016-01-13 14 views
0

我有下面的代码的文件config.js它:如何存根对象的属性,而不是方法?

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

我有下面的代码的另一个文件bus.js它:

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

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

现在,我想单元测试文件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(); 
     }); 
}); 

任何建议或帮助......

+0

所以......这是行不通的? (如果你修复了报价错字。)会发生什么?任何错误? –

+0

@ T.J. Crowder当我在测试中使用console.log(config.switch)时,它给了我null或undefined。 – Prajwal

+0

@ T.J.Crowder测试案例给了我bus.js其他部分的代码覆盖。 – Prajwal

回答

1

您需要REQ像这样使用你的模块var config=require('./config.js');

编辑:你应该改变你的要求呼叫以上。即使它代理为('config.js'),它在现实生活中也不起作用。你也可能需要以相同的方式调用总线,并像实际文件中那样构建配置对象。

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

我在测试文件中调用了'config'模块。 ,有没有什么办法可以覆盖原配置的属性值与我分配给它的任何值,并使用它进行测试? – Prajwal

+0

我无法提供值 – Prajwal

+0

工作...感谢队友... – Prajwal

1

在我看来,你可以在你的测试文件这样做:

var chai = require('chai'), 
    expect = chai.expect; 
var config = require('./config'); 

describe('Unit Test', function() { 

    it('should stub the config.switch', function(done) { 
    config.development.switch = true; 
    bus.getBusiness(req, function(data) { 
     ... 
     done(); 
    }); 
    }); 

}); 
相关问题