javascript - Sinon.spy 使用导入的方法失败
问题描述
我有几个 JS 模块,比如说 A 和 B。在模块 A 中,我有一个方法依赖于从模块 B 导入的另一个方法。现在我如何用 sinon.spy 测试,来自 A 的方法是否触发来自乙?
//ModuleA.js
import{ methodFromB } from "ModuleB.js";
function methodFromA (){
methodFromB();
}
export{
methodFromA
}
//ModuleB.js
function methodFromB (){
//doSomething
}
模块A.Spec.js
import sinon from 'sinon';
import { assert,expect } from "chai";
import * as modB from "ModuleB.js";
import { methodA } from '../js/ModuleA.js';
describe("ModuleA.js", function() {
beforeEach(function() {
stubmethod = sinon.stub(modB, "methodB").returns("success");
});
after(function() {
});
describe("#methodA", function() {
it("Should call the method methodB", function() {
expect(methodA()).to.deep.equal('success');
expect(stubmethod.calledOnce).to.equal(true);
});
});
});
在尝试存根 methodB 后,我收到错误“预期未定义到完全等于‘成功’”。
提前致谢。
解决方案
您从module B
. methodFromB
它不应该methodB
基于您的源文件。
describe("ModuleA.js", function () {
beforeEach(function () {
stubmethod = sinon.stub(modB, "methodFromB").returns("success"); // change to methodFromB
});
after(function () {
stubmethod.restore(); // don't forget to restore
});
describe("#methodA", function () {
it("Should call the method methodB", function () {
expect(methodA()).to.deep.equal('success');
expect(stubmethod.calledOnce).to.equal(true);
});
});
});
推荐阅读
- c# - 读取 CSV 文件并转换为转置表
- java - 使用 bytebuddy 更改对参数的方法访问
- c - 在 C 中扫描和打印 char 变量
- mysql - 获取mysql中没有数据或空值的表列
- scala - scala中getOrElse的用途是什么
- c# - Visual Studio 无法启动程序访问被拒绝
- javascript - fs.readFile 不是函数
- wordpress - 问:如何在 NGINX SSL 反向代理后面的官方 Wordpress Container 中配置 Apache
- numpy - Numpy最小二乘解决方案不准确的结果
- python - 来自文件夹的 Python 循环副本在 linux 中有效,但在 win 系统中无效