I tried several approaches and I cannot find the problem. This is the furthest I've come:
it('should load the current serviceData', fakeAsync( inject([Service1], (service1: Service1) => {
// spyOn(service1, 'get').and.returnValue(of(mockService1));
//spyOn(service1, 'get').and.callFake(() => { return of(mockService); });
spyOn(service1, 'get').and.callFake(() => {
console.log('get method called');
return of(mockService1);
});
component.ngOnInit();
tick(5000);
fixture.detectChanges();
expect(component.service2).toEqual(service1);
expect(service1.get).toHaveBeenCalledWith(0);
expect(component.mockService2).toEqual(mockService1);
expect(component.showCountdown).toBeFalse();
expect(component.loading).toBeFalse();
})));
The console.log statement is not reached, but:
expect(component.service2).toEqual(service1);
Is returning this error:
× should load the current campaign
Chrome Headless 83.0.4103.0 (Windows 10)
Expected object to have properties
get: spy on get
Expected object not to have properties
url: 'https://localhost:9091/data/dataService2.json'
Error: Expected object to have properties
get: spy on get
Expected object not to have properties
url: 'https://localhost:9091/data/dataService2.json'
at <Jasmine>
at UserContext.apply (src/app/features/home/home.component.spec.ts:88:41)
at TestBedImpl.execute (node_modules/@angular/core/fesm2020/testing.mjs:24217:19)
at UserContext.<anonymous> (node_modules/@angular/core/fesm2020/testing.mjs:24434:24)
I think my problem could be around this, but I am not able to find the reason of that. Please, any help
Well, forcing to the service to be the exactly the same service than the component works for me.
it('example', fakeAsync(inject([Service1], (service1: Service1) => {
spyOn(service1, 'get').and.callFake(() => {
return of(mockService1);
});
.......
component = new HomeComponent(sanitazer, service1, router, toastrService);
......
}