Angular

Angular Unit Testing

Jasmine, Karma, TestBed, component testing, service testing, mocking, spies, async testing, fixture

22 interview questionsยท
Mid-Level
1

What is the main role of TestBed in Angular unit testing?

Answer

TestBed is the primary utility for configuring and creating an Angular testing module. It allows configuring providers, imports, declarations and instantiating components in an isolated test environment. TestBed creates a test context that simulates how an Angular NgModule works, enabling dependency injection and component compilation.

2

Which TestBed method should be used to get a service instance?

Answer

TestBed.inject() is the recommended method to get a service instance from the testing module injector. This method replaces the older TestBed.get() syntax (deprecated) and provides better TypeScript typing. It accesses services provided in the TestBed configuration via configureTestingModule.

3

What does the TestBed.createComponent() method return?

Answer

createComponent() returns a ComponentFixture, a wrapper around the component that provides access to the component instance (componentInstance), its DOM element (nativeElement), DebugElement, and testing methods like detectChanges(). The ComponentFixture is the central object for testing an Angular component.

4

What is the role of fixture.detectChanges() in a component test?

5

How to create a Jasmine spy to mock a service method?

+19 interview questions

Master Angular for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free