I'm trying to write an unit test for a component that is defined in the main entry module. Since this component uses other components that are defined in shared module, I'd like to mock them instead of creating them. So, I've started by mocking the components. Here's an example:
@Component({ selector: "frt-dados-gerais", template: "" })
class DadosGeraisStubComponent {
@Input() utilizador: DadosUtilizadorInstancia;
@Input() gravacaoEmCurso = false;
@Output() atualizaDadosGerais = new EventEmitter<InfoDadosGerais>();
}
Now, since my shared module is exporting the frt-dados-gerais
component, I've tried to override the module with code that looks like this:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
NoopAnimationsModule,
RouterTestingModule.withRoutes(rotas),
BrowserModule,
ReactiveFormsModule,
PanelModule,
GrowlModule,
CoreModule,
SharedModule
],
declarations: [
DadosPessoaisComponent,
DadosGeraisStubComponent,
PalavraChaveStubComponent,
FuncoesDesempenhadasStubComponent,
IndisponibilidadeMotoristaStubComponent
],
providers: [
{ provide: UtilizadoresService, useValue: servicoUtilizadores },
{ provide: AutenticacaoService, useValue: servicoAutenticacao },
{ provide: ConfirmationService, useValue: utilizadoresService}
]
})
.overrideModule(SharedModule, {
remove: {
exports: [
DadosPessoaisComponent,
DadosGeraisComponent,
FuncoesDesempenhadasComponent,
IndisponibilidadeMotoristaComponent,
DadosGeraisComponent
]
}
})
As you can see, I've tried cancelling the exports so that I don't get and error saying that the component has two definitions. I'm clearly missing something because I'm always getting the NullInjectorError: No provider for DadosPessoaisComponent
error.
Can someone help?
EDIT: ok, finally figure it out. Such a stupid mistake...So, I was using TestBed.get
instead of TestBed.createComponent
to create the component under test...That's why I was getting the NullInjectorError
exception...