Always call compileComponents() when testing components to compile templates.
You can add imports to include Angular modules your component needs.
Use providers to add services or mocks for dependency injection.
Summary
TestBed configures a mini Angular environment for testing.
Use declarations, imports, and providers to set up what your test needs.
Call compileComponents() to prepare components before testing.
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?