我有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);
});