NestJS Jest 监听回调函数
所以我有一个具有功能的类。在这个函数中,我从具有回调函数的库的类对象中执行另一个函数。在该回调函数内,有一个我需要监视的方法。有什么办法可以做到这一点吗?这是粗略的细节:
export class Consume {
private readonly consumer: Consumer; // class from the library
private readonly randomService: RandomService; // class that it's method I want to spy on
constructor() {
this.consumer = new Consumer();
this.randomService = new RandomService();
}
consume(): void {
this.consumer.consume(async data => {
...
this.randomService.doSomething('withParam'); // I need to spy here on this method
...
})
}
}
我尝试过嘲笑,但似乎不起作用,这就是我尝试过的:
it('should do something', () => {
const consumerSpy = jest.spyOn(consumer, 'consume');
consumerSpy.mockImplementation(cb => {
cb(['data1', 'data2'])
}); // mocking the library's function
const randomServiceSpy = jest.spyOn(randomService, 'doSomething')
expect(randomServiceSpy).toBeCalledTimes(1); // Number of times called: 0 returned
});
还有:
it('should do something', () => {
jest.mock('path/to/consume-class', () => {
consume: jest.fn()
}); // mocking Consume's consume function
consume.consume(); // calling consume function from Consume class above
const randomServiceSpy = jest.spyOn(randomService, 'doSomething')
expect(randomServiceSpy).toBeCalledTimes(1); // Number of times called: 0 returned
});
任何帮助将不胜感激! :)
So I have a class which has a function. Inside this function, I execute another function from a library's class object which has a callback function. Inside that callback function, there's a method that I need to spy into. Is there any way to do that? Here's the rough detail:
export class Consume {
private readonly consumer: Consumer; // class from the library
private readonly randomService: RandomService; // class that it's method I want to spy on
constructor() {
this.consumer = new Consumer();
this.randomService = new RandomService();
}
consume(): void {
this.consumer.consume(async data => {
...
this.randomService.doSomething('withParam'); // I need to spy here on this method
...
})
}
}
I've tried mocking, but don't seem to work, here's what I have tried:
it('should do something', () => {
const consumerSpy = jest.spyOn(consumer, 'consume');
consumerSpy.mockImplementation(cb => {
cb(['data1', 'data2'])
}); // mocking the library's function
const randomServiceSpy = jest.spyOn(randomService, 'doSomething')
expect(randomServiceSpy).toBeCalledTimes(1); // Number of times called: 0 returned
});
and also this:
it('should do something', () => {
jest.mock('path/to/consume-class', () => {
consume: jest.fn()
}); // mocking Consume's consume function
consume.consume(); // calling consume function from Consume class above
const randomServiceSpy = jest.spyOn(randomService, 'doSomething')
expect(randomServiceSpy).toBeCalledTimes(1); // Number of times called: 0 returned
});
Any help would be appreciated! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过
jest.spyOn()
在Class.prototype.method
上安装间谍。例如
consume.ts
:lib.ts
:random.service.ts
:consume.test.ts
:测试结果:
You can use install spy on
Class.prototype.method
viajest.spyOn()
.E.g.
consume.ts
:lib.ts
:random.service.ts
:consume.test.ts
:Test result: