I'm writing TestBed unit tests.
There is a certain component, which is a child of the component-under-test. That child component causes errors while the test is running. That child component is not relevant to the test itself; it's just causing problems.
I would like to replace it with a dummy, or prevent it from being added.
The problematic component is from a module other than the component-under-test's module.
I tried to make a stub/dummy:
@Component({
selector : 'problematic-component-selector',
template : 'FAKE CAPTCHA',
})
export class ProblematicComponentStubComponent {
}
Here is my beforeEach
:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
ReactiveFormsModule,
FormsModule,
RouterModule,
ModuleOfProblematicComponent,
],
declarations: [
ComponentUnderTest,
ProblematicComponentStubComponent, /* NOTE: here I tried to declare the fake one */
],
providers: [
{ provide: Router, useClass: RouterStub },
{ provide: ActivatedRoute, useClass: Stub },
],
I tried to override the components template, to prevent the errors:
TestBed.overrideComponent(ProblematicComponent, {
set: {
template: 'Fake Captcha' // prevent ReCaptcha error
}
})
I know about NO_ERRORS_SCHEMA
, but it did not help.
I was also experimenting with overrideModule
, without success:
TestBed.overrideModule(ModuleOfProblematicComponent, {
remove: {
declarations: [ProblematicComponent],
},
add: {
declarations: [ProblematicComponentStubComponent],
}
});
So, question is: is it possible to override the ProblematicComponent
(which is in a module other than the module of the ComponentUnderTest
?
It is OK to re-configure TestBed after calling createComponent API on TestBed.
fixture. detectChanges() tells Angular to run change-detection. Finally! Every time it is called, it updates data bindings like ng-if, and re-renders the component based on the updated data. Calling this function will cause ngOnInit to run only the first time it is called.
The TestBed. createComponent() method is used to create an instance of the AppComponent. The spec then uses expect and matcher functions to see if the component produces the expected behavior. As a result, the spec will either pass or fail.
The TestBed is the first and largest of the Angular testing utilities. It creates an Angular testing module — a @NgModule class — that you configure with the configureTestingModule method to produce the module environment for the class you want to test.
The TestBed#overrideComponent
method will help you to replace template of any component within TestBed. If it's not enough, use TestBed#overrideModule
to replace the whole component (template and class).
Docs are loose: https://angular.io/api/core/testing/TestBed
But examples are more helpful: https://github.com/angular/angular/blob/master/aio/content/examples/testing/src/app/app.component.spec.ts#L74
Tests may help too: https://github.com/angular/angular/blob/master/packages/platform-browser/test/testing_public_spec.ts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With