In the ngOnInit method of the class I am testing I call a function of a service which retruns an observable. I have implemented a mock for the service, but I'm trying to use a spy for this exact test case. In my understanding the spy would overwrite the mock implementation unless I call ".and.callThrough()" on the spy. The problem is that everytime the mock implementation still gets executed although I set up a spy for the function.
I tried moving the spy into the beforeEach section which did not help. Also I tried to use the spy without the ".and.callFake()" extension. But it didn't help.
spec.ts file:
fdescribe('AppComponent', () => {
let fixture;
let component;
let dataServiceMock: DataServiceMock;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule],
declarations: [AppComponent],
providers: [{ provide: DataService, useClass: DataServiceMock }],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
fixture.detectChanges();
dataServiceMock = TestBed.get(DataService);
});
fit('should not show navigation if not logged in', async(() => {
spyOn(dataServiceMock,'getCurrentUser').and.callFake(() => {
console.log('IN CALL FAKE')
throwError(new Error('induced error'))
});
}));
implementation of service mock:
export class DataServiceMock {
currentUser: User;
private createValidUser() {
let validUser = new User();
validUser.username = 'valid';
validUser.password = 'valid';
validUser.role = 'valid';
this.currentUser = validUser;
}
public getCurrentUser(): Observable<User> {
this.createValidUser();
return of(this.currentUser);
}
ngOnInit of component that is tested:
ngOnInit(): void {
this.dataService.getCurrentUser().subscribe(user => {
this.currentUser = user;
console.log('received user:', this.currentUser)
})
}
I expect that the console log prints out "IN CALL FAKE" and throws the "induced error" but instead the console prints out "received user:" and the validUser that is created in the service mock.