Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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
✗ Incorrect
configureTestingModule is used to define declarations, imports, and providers for the test module.
What does TestBed.createComponent() return?
AA compiled module
BA component fixture with access to the component instance and DOM
CA service instance
DA promise
✗ Incorrect
createComponent returns a fixture that lets you access the component instance and its rendered DOM.
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
✗ Incorrect
useClass allows you to provide a different class (like a mock) instead of the real 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
✗ 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?
Aimports
Bproviders
Cdeclarations
Dexports
✗ 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.
Practice
(1/5)
1. What is the main purpose of Angular's TestBed in unit testing?
easy
A. To create a small Angular environment for testing components and services
B. To compile the entire Angular application for production
C. To replace Angular modules with plain JavaScript modules
D. To generate HTML templates automatically
Solution
Step 1: Understand TestBed's role
TestBed sets up a lightweight Angular environment to test parts of your app without running the full app.
Step 2: Compare options
Only To create a small Angular environment for testing components and services describes this testing environment purpose. Others describe unrelated tasks.
Final Answer:
To create a small Angular environment for testing components and services -> Option A
Quick Check:
TestBed purpose = create test environment [OK]
Hint: TestBed sets up Angular test environment, not full app build [OK]
2. Which of the following is the correct way to declare a component in TestBed configuration?
easy
A. TestBed.configureTestingModule({ imports: [MyComponent] })
B. TestBed.configureTestingModule({ bootstrap: [MyComponent] })
C. TestBed.configureTestingModule({ providers: [MyComponent] })
D. TestBed.configureTestingModule({ declarations: [MyComponent] })
Solution
Step 1: Identify where components go in TestBed
Components must be listed under declarations in the configuration.
Step 2: Check each option
Only TestBed.configureTestingModule({ declarations: [MyComponent] }) uses declarations with the component. Others misuse imports, providers, or bootstrap.
Final Answer:
TestBed.configureTestingModule({ declarations: [MyComponent] }) -> Option D
Quick Check:
Components go in declarations [OK]
Hint: Components go in declarations, modules in imports, services in providers [OK]
Common Mistakes:
Putting components inside imports or providers
Using bootstrap in TestBed config (only for app modules)
Forgetting to declare components causes errors
3. Given this TestBed setup, what will fixture.componentInstance.title output?