在先前测试中调用后看不到来自间谍的函数调用

时间:2018-12-20 13:38:37

标签: angular typescript unit-testing mocking spy

我有2个调用相同服务功能的功能。单独运行,通过每个功能的测试,但是顺序运行时,第二个失败,说明:

  

预期被调用一次的间谍unknown.serviceFunct。被打了0次。

myComp

funct1() {
  this.myService.serviceFunct('one');
}

funct2() {
  this.myService.serviceFunct('two');
}

myService

serviceFunct(term) {
  // Do something here
}

myTest

beforeEach(() => {
  // Here I am using createSpyObj and importing the correct provider to use the spy
  mockService = createSpyObj(['serviceFunct']);
});

it('test 1', () => {
  component.myFunct1();

  expect(mockService.serviceFunct).toHaveBeenCalledTimes(1);
});

it('test 2', () => {
  component.myFunct2();

  expect(mockService.serviceFunct).toHaveBeenCalledTimes(1);
});

0 个答案:

没有答案