要使用Jest进行模型化,我们通常使用jest.spyOn
。但是如何在Moleculer服务中模拟method
?
答案 0 :(得分:2)
只需在测试用例中创建服务后将其覆盖即可。 示例:
describe("Test MyService", () => {
const broker = new ServiceBroker({ logger: false });
const service = broker.createService(MyService);
// Mock my method in service instance
service.myMethod = jest.fn();
it("should call myMethod", () => {
service.doSomething();
expect(service.myMethod).toHaveBeenCalledTimes(1);
});
});