Skip to content

Bug: Standalone component with providers #5072

Answered by satanTime
GipHub123 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi there,

because ExampleService is a provider of a component, you cannot use TestBed.inject to get its instance, you should use the injector of the related DebugNode:

  beforeEach(async () => {
    fixture = TestBed.createComponent(StandaloneComponent);
    component = fixture.componentInstance;
    service = fixture.debugElement.injector.get(ExampleService);
  });

An example is here: https://codesandbox.io/s/great-estrela-uzlu88?file=/src/test.spec.ts:916-976

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by satanTime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
bug Something isn't working
2 participants
Converted from issue

This discussion was converted from issue #5013 on March 05, 2023 13:54.