ComponentFixture is the test handle for an Angular component. TestBed.createComponent returns it, allowing tests to reach the component instance, inspect its rendered host element, and synchronize template output with component state. section 29.4.1 section 29.4.2
Use the fixture properties and methods for these tasks:
- Read or modify the component through componentInstance. section 29.4.1
- Inspect the template through debugElement, then reach its DOM representation with nativeElement. section 29.4.2
- Call detectChanges() after changing component state so Angular reevaluates bindings and updates the template. section 29.4.2
- Use whenStable() when a test must wait for an operation's effects to be fully applied. section 29.4.1
Create a fixture and test a binding
Configure TestBed in beforeEach, create the component, and retain both handles: `let fixture: ComponentFixture<SimpleComponent>; let component: SimpleComponent; fixture = TestBed.createComponent(SimpleComponent); component = fixture.componentInstance;`. To test a rendered product count, locate a span through `fixture.debugElement` and `By.css`, change `component.category`, call `fixture.detectChanges()`, and assert that the span's `textContent` contains the updated count. Without detectChanges(), the template does not reflect the changed category. section 29.4.1 section 29.4.2
Table 29.5. Useful TestBed methods
|
Name
|
Description
|
|
configureTestingModule
|
This method is used to configure the Angular testing module.
|
|
createComponent
|
This method is used to create an instance of the component.
|
|
compileComponents
|
This method is used to compile components, as described in the “Testing a Component with an External Template” section.
|
Review the related TestBed operations for configuring a testing module, creating a component, and compiling components with external templates. section 29.4.1
