考虑到我有两门ES6课程.
这是A级:
import B from 'B';
class A {
someFunction(){
var dependency = new B();
dependency.doSomething();
}
}
B类:
class B{
doSomething(){
// does something
}
}
我正在使用mocha(用于ES6的babel)、chai和sinon进行单元测试,这非常有效.但是,在测试类a时,如何为类B提供模拟类呢?
我想模拟整个B类(或者所需的函数,实际上并不重要),这样A类就不会执行真正的代码,但我可以提供测试功能.
这就是目前的摩卡测试:
var A = require('path/to/A.js');
describe("Class A", () => {
var InstanceOfA;
beforeEach(() => {
InstanceOfA = new A();
});
it('should call B', () => {
InstanceOfA.someFunction();
// How to test A.someFunction() without relying on B???
});
});