javascript - Sinon:模拟一个由命名导出调用的内部函数
问题描述
我必须模拟一个内部调用的函数,但我正在测试的函数是使用打字稿中的命名导出导出的。
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();
}
}
您能否建议一种模拟该方法的internalFunc()
方法?
解决方案
嗯,我自己找到了方法。不确定这是否是解决此问题的正确方法。
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
,那将会很有帮助。
推荐阅读
- php - 警告:http_build_query():参数 1 应为数组或对象(但它有效)
- powershell - 恢复 Powershell 功能
- vba - ma 访问:发送对象出错返回表单
- python - 使用 pip 安装 discord.py 的 Python 问题
- javascript - 嵌套的 javascript 事件侦听器是否有更简洁的模式?
- c# - 我只在视图模型类中获得组合框选定项,但不能在视图模型类之外使用它,它的抛出为空
- prolog - Prolog 压缩与合并排序
- api - 我是否应该假设 API 总是返回其文档所说的内容?
- scala - 用于递归泛型定义的 Scala 通配符
- svelte - 如果数据不存在或已经使用 Svelte / Sapper 获取了 svelte 类型的 store id 数据,如何有效地从 rest api 获取数据