笑话:如何将参数传递给模拟构造函数?

时间:2019-01-27 03:22:06

标签: javascript jestjs

如果我想创建ES6类实例方法的模拟实现,

// ExampleClass.js
export class ExampleClass {
    constructor(someValue) {
        this.a = someValue;
    }

    exampleMethod(anotherValue) {
        // do something with 'anotherValue'
    }
}

// OtherModule.js
import {ExampleClass} from './ExampleClass';
export const fooBar = () => {
    const ex = new ExampleClass("hello world");
    ex.exampleMethod("another value");
};

// ExampleClass.test.js
import {fooBar} from './OtherModule';
import {ExampleClass} from './ExampleClass';
jest.mock('./ExampleClass');

it('try to create a mock of ExampleClass', () => {
    ExampleClass.mockClear();

    fooBar();

    // to verify values for of instance method "exampleMethod" of ExampleClass instance
    expect(ExampleClass.mock.instances[0].exampleMethod.calls.length).toBe(1);
    expect(ExampleClass.mock.instances[0].exampleMethod.calls[0][0]).toBe("another value");

    // How to verify values for **constructor** of ExampleClass ?????
    // expect(ExampleClass.mock.instances[0].constructor.calls.length).toBe(1);
    // expect(ExampleClass.mock.instances[0].constructor.calls[0][0]).toBe("another value");
});

我不知道该怎么做(以及在注释的代码中提到的某种方式)是如何监视/访问构造函数的值(而不仅仅是实例方法)。

任何帮助将不胜感激! ❤

1 个答案:

答案 0 :(得分:0)

Dim rngLastCell As Range Set rngLastCell = rngOrigin(rngOrigin.Count) 是构造函数,由于整个模块都是自动模拟的,因此已经将其设置为模拟函数:

ExampleClass
相关问题