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 }] });

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.

Further reading

Problem with a question

Angular · Senior · question 47

Test yourself

Take the Angular · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz