Q1. What tools are used for testing Angular applications?
Angular uses Jasmine as the testing framework (with describe, it, expect) and Karma as the test runner. TestBed is Angular''s primary API for unit testing components and services. For end-to-end testing, Protractor was used traditionally, but Cypress or Playwright are now common.
Q2. How do you test a component in isolation?
Use TestBed.configureTestingModule to configure the testing module. Then create the component with TestBed.createComponent. Example:
let fixture = TestBed.createComponent(MyComponent);
let component = fixture.componentInstance;
fixture.detectChanges();
expect(component.title).toBe(''My App'');
Q3. How do you test a service with dependencies?
Use TestBed.inject to get the service. If the service depends on HttpClient, you can use HttpClientTestingModule and HttpTestingController. Example:
TestBed.configureTestingModule({ imports: [HttpClientTestingModule] });
service = TestBed.inject(MyService);
httpMock = TestBed.inject(HttpTestingController);
Q4. What is a spy in Jasmine?
A spy is a mock function that tracks calls and arguments. Used to test if methods were called, with specific arguments. Example:
spyOn(service, ''getData'').and.returnValue(of([]));
expect(service.getData).toHaveBeenCalled();
Q5. How do you test DOM interaction?
Use fixture.nativeElement to query the DOM. Example:
const button = fixture.nativeElement.querySelector(''button'');
button.click();
fixture.detectChanges();
expect(fixture.nativeElement.querySelector(''p'').textContent).toContain(''Clicked'');
