Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
Dependency injection in parent and child component in Angular

I was having issues with a test in angular . The strange thing was the tests succeeded with

ng test

and failed with

ng test --watch=false --browsers=ChromeHeadless --code-coverage

But adding constructor in the child component solved the issue.

Parent component :

 Component({
  templateUrl: './edit-organization.component.html',
  styleUrls: ['./edit-organization.component.scss']
})
export class EditOrganizationComponent implements OnInit {
  organization: Organization;
  errorReason: ErrorReason;
  organization$: Observable<Organization>;
  errorCodes: ErrorCode[];

  constructor(
    protected currentUserRoute: ActivatedRoute,
    protected organizationService: OrganizationService,
    protected navigationService: NavigationService
  ) { }

  ngOnInit(): void {
    this.organization = this.currentUserRoute.snapshot.data.organization;
  }

  updateOrganization(form: NgForm) { ...}

Child component 1 (The test used to fail here)

@Component({
  templateUrl: './edit-business-organization-profile.component.html',
})
export class EditBusinessOrganizationProfileComponent
  extends EditOrganizationComponent
  implements OnInit
{
  organizationServiceTypes: OrganizationServiceType[] =
    organizationServiceTypes;

  constructor(readonly currentUserRoute: ActivatedRoute,
              readonly organizationService: OrganizationService,
              readonly navigationService: NavigationService) {
    super(currentUserRoute, organizationService, navigationService);
  }
  ngOnInit(): void {
    super.ngOnInit();
  }
}

Child component 2 (The tests pass here)

@Component({
  templateUrl: './business-organization-profile.component.html',
})
export class BusinessOrganizationProfileComponent
  extends EditOrganizationComponent
  implements OnInit {
  ngOnInit(): void {
    super.ngOnInit();
  }
}

I have a almost similar test for both the component which were passing before but when I added a field organizationServiceTypes in EditBusinessOrganizationProfileComponent the tests started failing .

Earlier I didn't have any constructor in the component EditBusinessOrganizationProfileComponent, once I added the constructor the tests started getting passed.

Can someone explain how the dependency injection works in this case as the code works perfectly without the test , it is only the test that breaks ?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda