首页 > 解决方案 > 笑话 2 级深度

问题描述

我正在尝试从 A 类测试一个函数 funcA,该函数调用 B 类的 funcB,而 B 类的 funcB 又调用 C 类的 funcC。

class A {
  funcA(){
   const result = new B().funcB();
}}

class A {
  funcA(){
   const result = new C().funcc()+d;
}}

class C {
  funcC(){
   return a+b;
}}

测试课是这样的:

describe('class A testing', () => {

jest.mock('../../../impl/b');
jest.mock('../../../impl/b', () => {
    funcB : jest.fn().mockImplementation( () => {return 5})
});
jest.mock('../../../impl/c', () => {
    funcB : jest.fn().mockImplementation( () => {return 6})
});
it("testing funcA", () => {
     const a = new A(context);
     const val = a.funcA();
     expect(val).toEqual(11);
})
}
)

所以我看到调用仍然是原始函数,而不是模拟的 funcB 和 funcC。我想知道我在哪里嘲笑错了?

标签: node.jsunit-testingjestjs

解决方案


推荐阅读