Javascript 如何存根a";包装纸;使用Sinon的函数?

Javascript 如何存根a";包装纸;使用Sinon的函数?,javascript,node.js,lambda,sinon,Javascript,Node.js,Lambda,Sinon,我正在设置一个Lambda函数(node.js),举个例子,我们将使其最小化 module.exports = (event, context, callback) { console.log("hello world") } 然而,我已经创建了一个函数来包装lambda函数,它允许我在每个lambda执行之前执行一些必需的函数(我有一个lambda函数集合,这些函数是使用它们的函数连接起来的)。它还允许我整合每个函数中的一些日志记录和错误处理 // hook.js const conne

我正在设置一个Lambda函数(node.js),举个例子,我们将使其最小化

module.exports = (event, context, callback) {
  console.log("hello world")
}
然而,我已经创建了一个函数来包装lambda函数,它允许我在每个lambda执行之前执行一些必需的函数(我有一个lambda函数集合,这些函数是使用它们的函数连接起来的)。它还允许我整合每个函数中的一些日志记录和错误处理

// hook.js
const connect = fn => (event, context, callback) => {
  someFunction()
    .then(() => fn(event, context, callback))
    .then(res => callback(null, res))
    .catch(error => {
      // logging
      callback(error)
    })
}

module.exports = { connect }

// index.js
const Hook = require("./hook")

exports.handler = Hook.connect((event, context, callback) => {
  console.log("hello world")
})
逻辑运行良好,Lambda正在成功地处理它。但是,我正在尝试使用这个
钩子连接
函数,需要一些指导

我只想将其存根以返回一个已解析的承诺,这样我们就可以继续处理每个Lambda函数中的代码(
fn(事件、上下文、回调)

我尝试了几种不同的方法,从基本的,
sinon.stub(Hook,“connect”)
,到更复杂的方法,我尝试在
Hook.js
文件中使用stub来存根私有函数


如果您有任何帮助,我们将不胜感激,请提前向您表示感谢。

这里是一个工作测试:

const sinon = require('sinon');
const Hook = require('./hook');

const event = {}; // for simplicity sake
const context = {}; // for simplicity sake
const callback = {}; // for simplicity sake

describe('Hello', () => {

  let handler, connectStub;
  before(() => {
    connectStub = sinon.stub(Hook, 'connect');
    connectStub.callsFake(fn => (...args) => fn(...args));  // create the mock...
    delete require.cache[require.resolve('./index')];  // (in case it's already cached)
    handler = require('./index').handler;  // <= ...now require index.js
  });

  after(() => {
    connectStub.restore();  // restore Hook.connect
    delete require.cache[require.resolve('./index')];  // remove the modified index.js
  });

  it('works', () => {
    const results = handler(event, context, callback);  // it works!
    // assert
  });
});
const sinon=require('sinon');
const Hook=require(“./Hook”);
常量事件={};//为了简单起见
常量上下文={};//为了简单起见
常量回调={};//为了简单起见
描述('你好',()=>{
让处理器,连接存根;
之前(()=>{
connectStub=sinon.stub(Hook,'connect');
connectStub.callsFake(fn=>(…args)=>fn(…args));//创建模拟。。。
删除require.cache[require.resolve('./索引')];/(如果已经缓存)
handler=require('./索引')。handler;//{
connectStub.restore();//restore Hook.connect
删除require.cache[require.resolve('./index')];//删除修改后的index.js
});
它('有效',()=>{
const results=handler(事件、上下文、回调);//有效!
//断言
});
});
详细信息

index.js
调用
Hook.connect
以在运行时创建导出的
处理程序
,并在
需要时运行

…因此,
Hook.connect
的模拟需要在
index.js
所需之前就位:


因此,此测试还将在测试之前和之后清除Node.js缓存,以确保
index.js
拾取
Hook.connect
mock,并确保在以后需要真正的
index.js
时,从缓存中删除带有模拟的
Hook.connect的
index.js

real非常感谢您在这里的时间以及您周到/详细的回复。一切如期进行!
const sinon = require('sinon');
const Hook = require('./hook');

const event = {}; // for simplicity sake
const context = {}; // for simplicity sake
const callback = {}; // for simplicity sake

describe('Hello', () => {

  let handler, connectStub;
  before(() => {
    connectStub = sinon.stub(Hook, 'connect');
    connectStub.callsFake(fn => (...args) => fn(...args));  // create the mock...
    delete require.cache[require.resolve('./index')];  // (in case it's already cached)
    handler = require('./index').handler;  // <= ...now require index.js
  });

  after(() => {
    connectStub.restore();  // restore Hook.connect
    delete require.cache[require.resolve('./index')];  // remove the modified index.js
  });

  it('works', () => {
    const results = handler(event, context, callback);  // it works!
    // assert
  });
});