Complete the code to create a basic Angular test setup for a component.
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [[1]]
}).compileComponents();
});The declarations array should include the component you want to test, such as AppComponent.
Complete the code to create a test that checks if the component is created successfully.
it('should create the component', () => { const fixture = TestBed.createComponent(AppComponent); const component = fixture.componentInstance; expect(component).[1]; });
Using toBeTruthy() checks that the component instance exists and is truthy, meaning it was created.
Fix the error in the test by completing the code to detect changes after setting a component property.
it('should update title in template', () => { const fixture = TestBed.createComponent(AppComponent); const component = fixture.componentInstance; component.title = 'Test Title'; fixture.[1](); const compiled = fixture.nativeElement as HTMLElement; expect(compiled.querySelector('h1')?.textContent).toContain('Test Title'); });
Calling fixture.detectChanges() tells Angular to update the template with the latest component data.
Fill both blanks to import necessary modules and declare the component in the test setup.
beforeEach(() => {
TestBed.configureTestingModule({
imports: [[1]],
declarations: [[2]]
}).compileComponents();
});HttpClientTestingModule is imported to mock HTTP requests, and AppComponent is declared to test it.
Fill all three blanks to create a test that spies on a service method and checks if it was called.
it('should call fetchData from service', () => { const fixture = TestBed.createComponent(DataComponent); const component = fixture.componentInstance; const service = TestBed.inject(DataService); spyOn(service, '[1]').and.returnValue(of([])); component.loadData(); expect(service.[2]).toHaveBeenCalled(); expect(component.data).toEqual([3]); });
The test spies on the fetchData method, expects it to be called, and checks that the component's data is an empty array [] as returned by the spy.