0

我正在监视inemit的方法EventEmitterAngular

spyOn(answerComponent.answerEmitter, 'emit');

我想检查是否emit使用参数调用,A但我不想检查与A. 我想检查emit用值调用A.a, A.b并忽略A.c.

有可能这样做吗?

4

1 回答 1

0

我想到了两种方法:

一个通过使用本机toHaveBeenCalledWith

expect(answerComponent.answerEmitter, 'emit').toHaveBeenCalledWith(A.a);
expect(answerComponent.answerEmitter, 'emit').toHaveBeenCalledWith(A.b);
// you can think of all the callers being tracked as an array and you can assert with
// toHaveBeenCalledWith to check the array of all of the calls and see the arguments
expect(anserComponent.anserEmitter, 'emit').not.toHaveBeenCalledWith(A.c); // maybe you're looking for this as well

您还可以监视发射并调用假函数:

spyOn(answerComponent.answerEmitter, 'emit').and.callFake((arg: any) => {
  // every time emit is called, this fake function is called
  if (arg !== A.a || arg !== A.b) {
     throw 'Wrong argument passed!!'; // you can refine this call fake
  }  // but the point is that you can have a handle on the argument passed and assert it
});
于 2020-12-29T02:59:13.040 回答