Angular Unit Testing
Jasmine, Karma, TestBed, component testing, service testing, mocking, spies, async testing, fixture
1What is the main role of TestBed in Angular unit testing?
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.
2Which TestBed method should be used to get a service instance?
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.
3What does the TestBed.createComponent() method return?
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.
What is the role of fixture.detectChanges() in a component test?
How to create a Jasmine spy to mock a service method?
+19 interview questions
Other Angular interview topics
TypeScript Basics
TypeScript Advanced
Angular Fundamentals
Components & Lifecycle
Services & Dependency Injection
Angular Modules Organization
Angular CLI
Directives & Pipes
Routing & Navigation
Reactive Forms
Template-driven Forms
RxJS Fundamentals
RxJS Operators
HttpClient & API Calls
Basic State Management
Change Detection
Angular Signals
Standalone Components
End-to-End Testing
Build & Optimization
NgRx Fundamentals
NgRx Advanced
Angular Architecture
Performance Optimization
Security & Best Practices
Advanced RxJS Patterns
Angular Universal & SSR
Angular Micro-frontends
Master Angular for your next interview
Access all questions, flashcards, technical tests, code review exercises and interview simulators.
Start for free