0
0
Angularframework~5 mins

TestBed configuration in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AconfigureTestingModule
BcreateComponent
CcompileComponents
Dinject
What does TestBed.createComponent() return?
AA compiled module
BA component fixture with access to the component instance and DOM
CA service instance
DA promise
Why might you use useClass in TestBed providers?
ATo declare a component
BTo import a module
CTo compile templates
DTo replace a real service with a mock service
When should you call compileComponents() in TestBed?
AAfter configureTestingModule and before creating components
BBefore configureTestingModule
CAfter creating components
DIt is optional and never needed
Which array in TestBed configuration holds components you want to test?
Aimports
Bproviders
Cdeclarations
Dexports
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.