问题描述
我必须模拟一个内部调用的函数,但是我正在测试的函数是使用打字稿中的命名导出导出的。
import { internalFunc } from './internal.ts';
const funcToTest = () => {
internalFunc(); // I need to mock this function
}
export {
funcToTest
}
现在我的测试文件如下:
import { describe } from 'mocha';
import { expect } from 'chai';
import sinon from 'sinon';
import { funcToTest } from './myModule.ts';
describe ('something meaningful',() => {
it ('should pass',() => {
sinon.stub(); // I'm stuck here. How do I mock this internalFunc()?
let result = funcTotest();
}
}
解决方法
好吧,我自己找到了一条路。不确定这是否是解决此问题的正确方法。
import { describe } from 'mocha';
import { expect } from 'chai';
import sinon from 'sinon';
import { funcToTest } from './myModule.ts';
import * as internal from './internal.ts';
describe ('something meaningful',() => {
it ('should pass',() => {
sinon.stub(internal,'internalFunc').returns('some value');
let result = funcToTest();
}
}
如果有人发现了一种更好的模拟此internalFunc
的方法,将会很有帮助。