我有下面的代碼的文件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