我正在尝试在SailsJS 1.0应用程序中设置单元测试。我想模拟数据库,并且not必须进行测试。
我有一个查询数据库的非常简单的actions2(节点计算机)帮助程序:
fn: async function (inputs, exits) {
Users.findOne({id: inputs.userId})
.exec((err, data) => {
if (err) {
return exits.error(err);
}
if (!data) {
return exits.success([]);
}
return exits.success(data);
});
}
我正在使用摩卡咖啡作为测试框架。如何模拟用户?
您可以使用sinon附加模型的功能。
const sinon = require('sinon');
it('should respond 200 WHEN accessId and secretKey are valid', async () => {
const sandbox = createSandbox();
const User = require('/path/to/User');
sandbox.stub(User, 'findOne')
.returns({
exec: (callback) => {
callback(null, { data: 'you want returned' })
}
})
sandbox.restore(); // restores User.findOne to its original functionality so that other tests will not be contaminated
});