我正在尝试测试vue组件,其中在创建的钩子中进行了服务调用,并传递了来自vue组件的methods对象的函数。例如
....
created() {
GlobalMessageRegistry.addUpdateListener(this.update)
},
methods: {
update(updateMessages) {
//do stuff
},
},
....
我想测试通过传递this.update来调用GlobalMessageRegistry.addUpdateListener,以便我的笑话测试看起来像这样(注意GlobalMessageRegistry在测试中已正确模拟)
....
it('Adds update as a GlobalMessageRegistry update listener, () => {
const mockUpdate = jest.fn();
const component = shallowMount(MyComponent, {
methods: {
update: mockUpdate
},
});
expect(GlobalMessageRegistry.addUpdateListener).toHaveBeenCalledWith(mockUpdated);
});
....