我有下面的代码的文件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();
});
});
任何建议或帮助......
所以......这是行不通的? (如果你修复了报价错字。)会发生什么?任何错误? –
@ T.J. Crowder当我在测试中使用console.log(config.switch)时,它给了我null或undefined。 – Prajwal
@ T.J.Crowder测试案例给了我bus.js其他部分的代码覆盖。 – Prajwal