Writing a unit test for a component, that uses a Service with an emit inside the constructor, like this:
@Injectable()
export class Service1 {
public onService1Done: EventEmitter<any> = new EventEmitter();
public constructor(...) {
this.onService1Done.emit(value);
}
}
I notice that, according to what i have in the component.spec.ts:
beforeEachProviders(() => {
return [
provide(Service1, { useClass: MockService1 }),
Component1
];
});
it("check that XXX", inject(
[Service1, Component1], (service1: Service1, component1: Component1) => {
service1.onService1Done.subscribe({ next: () => DoSomething() });
expect(DoSomething()).toEqual(IsDone);
}));
});
}
The constructor of Service1, and so the emit, will be called before i could make the subscribe, inside the test;
there is a way to avoid this ?? to make the subscribe before the Constructor ?
As always, thanks in advance.
I don't see how anything could subscribe in any way to get this event when the service instance is created by DI.
Just don't emit it in the constructor or at least use some setTimeout(...)
than at least synchronically executing code has a chance to subscribe if you must.