Apologies for my inexperience with Angular, but I am struggling with using spyOn in a unit test.
In my unit test, there is a method on the component that calls service1, which in turn calls another service2.
However, when I try to spyOn service1 in order to use a mock value, it ends up calling the actual ServiceProvidersHTTPService.getAllUsers instead of MockAppConfigService as intended.
Below is an excerpt from my Test:
describe('ProjectAnalystComponent', () => {
let component: ProjectAnalystComponent;
let fixture: ComponentFixture<ProjectAnalystComponent>;
let service: ServiceProvidersHTTPService ;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpClientModule, RouterTestingModule],
declarations: [ ProjectAnalystComponent ],
providers:
[
ServiceProvidersHTTPService,
CurrentCreateProjectService,
NotificationService,
MessageService,
ProjectLeadAnalystHTTPService,
ExceptionService,
{provide: AppConfigService, useClass: MockAppConfigService }
],
schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ProjectAnalystComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
console.log("######### Did this run");
let service = fixture.debugElement.injector.get(ServiceProvidersHTTPService);
spyOn(service, 'getAllUsers').and.returnValue('{}');
expect(component).toBeTruthy();
});
});