我有一个文件 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();
});
});
任何建议或帮助...
最佳答案
在我看来,您可以在测试文件中执行此操作:
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();
});
});
});
关于javascript - 如何 stub 对象的属性而不是方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34772533/