Node.js 断言传入存根函数的函数是正确的函数

Node.js 断言传入存根函数的函数是正确的函数,node.js,mocha.js,sinon,stub,Node.js,Mocha.js,Sinon,Stub,我正在尝试使用Mocha测试我的节点模块 模块非常小,这里是一个示例 import { sharedFunctionA, sharedFunctionB, commonFunction } from <file> const functionA = token => _data => sharedFunctionA(token); const functionB = () => data => sharedFunctionB(data); exports.

我正在尝试使用Mocha测试我的节点模块

模块非常小,这里是一个示例

import { sharedFunctionA, sharedFunctionB, commonFunction } from <file>

const functionA = token => _data => sharedFunctionA(token);
const functionB = () => data => sharedFunctionB(data);

exports.doThingA = token => {
  commonFunction(functionA(token));
};

exports.doThingB = () => {
  commonFunction(functionB());
};
从导入{SharedFunction A、SharedFunction B、commonFunction}
const function=token=>\u data=>sharedFunctionA(token);
const functionB=()=>data=>sharedFunctionB(数据);
exports.doThingA=令牌=>{
commonFunction(功能(令牌));
};
exports.doThingB=()=>{
commonFunction(functionB());
};
这只是一个简单的例子,但它显示了我正在尝试做什么

我需要测试
doThingA
doThingB
是否将正确的函数传递给
commonFunction

我已经在
commonFunction
上打了存根,我可以看到它正在被调用,但我不能断言传入的函数是正确的


TBH。。。我开始考虑重新构造它,将某种枚举传递到
commonFunction
,并从那里运行相应的函数。

在这种情况下,您可以在
sharedFunctionA
sharedFunctionB
上存根,然后在
commonFunction
上检索存根的参数,并叫它。然后检查是否正在使用所需参数调用其他存根

我知道这很枯燥,但这是我能想到的使用你的代码的唯一方法

快速示例:

const assert = require('assert')
const sinon = require('sinon')
const sharedFunctions = require('<fileWithSharedFunctions>')
const commonStub = sinon.stub(sharedFunctions, 'commonFunction')
const sharedBStub = sinon.stub(sharedFunctions, 'sharedFunctionB')

const fileToTest = require('<fileToTest>')

fileToTest.doThingB()
commonStub.getCall(0).args[0]()
assert(sharedBStub.calledOnce)
const assert=require('assert'))
const sinon=require('sinon')
const sharedFunctions=require(“”)
const commonStub=sinon.stub(SharedFunction,'commonFunction')
const sharedBStub=sinon.stub(sharedFunctions,'sharedFunctionB')
const fileToTest=require(“”)
fileToTest.doThingB()
commonStub.getCall(0.args[0]()
断言(sharedBStub.calledOnce)

啊!我没想到。谢谢,我会试一试的。我用了一种稍微不同的方法,但现在它开始工作了。谢谢:D