Within my Angular application, I have a service that is specifically provided at the component level:
@Component({
selector: 'my-component',
templateUrl: './my.component.html',
providers: [MyService],
})
export class MyComponent implements OnInit, OnDestroy { ... }
When testing this component with spectator in Angular tests, I am encountering an issue where mocking the service does not seem to affect the component-level instance:
const createComponent = createComponentFactory({
component: MyComponent,
providers: [
// ... other services ...
mockProvider(MyService),
],
// ...
});
// ...
const spectator = createComponent();
const myService = spectator.inject(MyService);
It appears that despite my efforts to mock behaviors on myService
, the global instance of MyService
is being accessed instead of the component-level instance.