参考:Integration testing with mongojs to cover database errors
链接的场景与使用mongoskin的我的集成测试相同,以覆盖db错误。
var mongoskin = require('mongoskin');
var db = require('./setting').DB;
var collection = db.collection('test');
var route = require('express').Router();
...
route.post('test', function(req,res) {
db.collection('test').update({...}, {...}, function (err, data) {
if (err) {
// this code should be tested
logger.error(err);
return;
}
// all is good, this is usually covered
});
});
file.test.js
var sinon = require('sinon');
var rewire = require('rewire');
var tc = rewire('./file');
var superagent = require('superagent');
var mongoskin = require('mongoskin');
var db = tc.__get__('db');
it('xx', function(done){
var update = sinon.stub().yields(new Error('error'));
sinon.stub(db, 'collection').returns({update: update})
superagent.post(url).send(data).end(function(e,res){
done();
});
})
我尝试但无效。
帮助。我希望模拟更新mongodb并将错误设置为第3个arg,logger输出尖叫。