我遇到了一个问题,即试图监视构造函数中调用的服务函数调用。测试是基本的,只是验证函数调用是否实际被调用。
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
providers: [TestService]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
let service = fixture.debugElement.injector.get(TestService);
component = fixture.componentInstance;
spyOn(service , "start").and.callThrough();
fixture.detectChanges();
});
it('start gets called', () => {
expect(service .start).toHaveBeenCalled();
})
至于AppComponent,在构造函数中它只是调用服务。开始()
我认为问题在于,spyOn在创建组件后被调用,但在注入服务之前,我如何监视服务?ie:
fixture = TestBed.createComponent(AppComponent);
let service = fixture.debugElement.injector.get(TestService);