gpt4 book ai didi

javascript - 单元测试 Sails JS 1.0 助手

转载 作者:行者123 更新时间:2023-12-01 15:47:40 26 4
gpt4 key购买 nike

我正在尝试在我的 SailsJS 1.0 应用程序中设置单元测试。我想模拟数据库,而不必运行帆升降机进行测试。

我有一个非常简单的 actions2( Node 机器)助手来查询数据库:

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);
});
}

我使用 mocha 作为我的测试框架。如何模拟用户?

最佳答案

您可以使用 sinon stub 模型的功能。

const sinon = require('sinon');

it('should something', 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
});

关于javascript - 单元测试 Sails JS 1.0 助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47407689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com