Angular · Senior · Question 47 of 75
Why doesn't this test replace the service used by the component?
@Component({ providers: [EditorState] }) class Editor {}
TestBed.configureTestingModule({ providers: [{ provide: EditorState, useValue: fake }] });- ✓Correct answer: Component-level providers take priority; use
TestBed.overrideComponentto replace them - 2
useValuedoesn't work in tests - 3TestBed providers apply only to pipes
Explanation
The component's element injector is closer than the test module's injector. TestBed.overrideComponent(Editor, { set: { providers: [{ provide: EditorState, useValue: fake }] } }) fixes it.