【发布时间】:2018-08-02 05:00:06
【问题描述】:
我有几个 JS 模块,比如说 A 和 B。在模块 A 中,我有一个方法依赖于从模块 B 导入的另一个方法。现在我如何用 sinon.spy 测试,是否来自 A 的方法触发B的方法?
//ModuleA.js
import{ methodFromB } from "ModuleB.js";
function methodFromA (){
methodFromB();
}
export{
methodFromA
}
//ModuleB.js
function methodFromB (){
//doSomething
}
ModuleA.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 后,我收到错误“预期未定义到完全等于‘成功’”。
提前致谢。
【问题讨论】:
标签: javascript unit-testing sinon sinon-chai