Recall & Review
beginner
What is TestBed in Angular testing?
TestBed is Angular's primary API for writing unit tests. It allows you to configure and create an Angular testing module that mimics an Angular @NgModule, so you can test components, services, and other parts in isolation.
Click to reveal answer
beginner
How do you configure a testing module with TestBed?
You use
TestBed.configureTestingModule({ declarations: [...], imports: [...], providers: [...] }) to set up components, modules, and services needed for your test.Click to reveal answer
intermediate
Why do you call
TestBed.compileComponents() after configuring TestBed?Calling
compileComponents() compiles the declared components' templates and CSS asynchronously. This is needed before creating component instances in tests.Click to reveal answer
beginner
What is the purpose of
TestBed.createComponent()?It creates an instance of a component along with its template and styles inside the testing environment. This lets you interact with the component as if it was running in the app.
Click to reveal answer
intermediate
How can you provide a mock service in TestBed configuration?
In the
providers array, use { provide: RealService, useClass: MockService } to replace the real service with a mock version during tests.Click to reveal answer
Which TestBed method sets up the testing module with components and services?
✗ Incorrect
configureTestingModule is used to define declarations, imports, and providers for the test module.
What does
TestBed.createComponent() return?✗ Incorrect
createComponent returns a fixture that lets you access the component instance and its rendered DOM.
Why might you use
useClass in TestBed providers?✗ Incorrect
useClass allows you to provide a different class (like a mock) instead of the real service.
When should you call
compileComponents() in TestBed?✗ Incorrect
compileComponents compiles templates and must be called after configuration but before creating components.
Which array in TestBed configuration holds components you want to test?
✗ Incorrect
declarations is where you list components, directives, and pipes used in the test.
Explain how to set up TestBed to test an Angular component with a mock service.
Think about declarations, providers, compileComponents, and createComponent steps.
You got /4 concepts.
Describe the role of TestBed in Angular unit testing and why it is important.
Focus on how TestBed mimics Angular's environment for tests.
You got /4 concepts.