我需要复制间谍的功能,这是通过一个事件调用。也就是说,假设存在:spyOn(method).and.callThrough(),是否也存在类似于:spyOnEvent(event).and.triggerThrough()的东西
发布于 2021-04-06 14:33:09
该事件的作用类似于函数调用,因此您可以用相同的方式设置它。
例如,当你设置你的组件,并且有一个函数(onInit),你希望另一个函数(myFunction)作为它的一部分被调用。
// arrange
spyOn(component, 'myFunction');
// act
component.onInit();
// assert
expect(component.myFunction).toHaveBeenCalled();因此,您可以以相同的方式设置事件,其中事件(myEvent)在被测试的函数(myFunction)内触发:
// arrange
spyOn(component.myEvent, 'emit');
// act
component.myFunction();
// assert
expect(component.myEvent.emit).toHaveBeenCalled();只有当您的服务有一个预期要执行的调用时,您才会正常设置数据,因为您的测试范围需要一些返回数据:
spyOn(myService, 'myServiceFunction').and.returnValue(of(true));或者,您可以将其设置为:
mockedService.spyOf(x => x.myServiceFunction).and.returnValue(of(true));https://stackoverflow.com/questions/66879703
复制相似问题