单元测试Sails JS 1.0助手

时间:2017-11-21 07:39:59

标签: javascript node.js unit-testing mocking sails.js

我试图在我的SailsJS 1.0应用程序中设置单元测试。我想嘲笑数据库而必须运行sails lift来测试。

我有一个非常简单的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);
    });
}

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

1 个答案:

答案 0 :(得分:0)

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

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
});
相关问题