【发布时间】:2014-11-06 02:28:04
【问题描述】:
我有一个看起来像这样的 Sails.Js 控制器
module.exports = {
confirmID: function(req,res) {
var uid = req.params.id;
User.findOne({id:uid}).exec(function(err,user) {
// ...
});
}
}
User 是一个sails-postgres 模型。我试过用 mocha、sinon 和 supertest 测试它,用这样的测试
describe('Controller', function() {
var sandbox;
before(function() {
sandbox = sinon.sandbox.create();
sandbox.stub(User, 'findOne');
});
after(function() {
sandbox.restore();
});
describe('GET /confirmid/:id', function() {
it('should do something', function(done) {
request(sails.hooks.http.app)
.get('/confirmid/123')
.expect(200)
.end(function(err,res) {
sandbox.fakes[0].called.should.be.true;
done();
});
});
});
如果我把它留在那个地方,它会出错,因为 exec 在未定义时被调用,但我似乎无法在没有错误或测试挂起的情况下存根嵌套的 exec 方法。有没有办法存根一系列方法调用,例如.find().exec()?还是我最好将其留给可以使用实际数据库进行测试的集成测试?
【问题讨论】:
标签: node.js orm sails.js sinon