Bird
0
0

What is wrong with this test setup code?

medium📝 Debug Q14 of 15
Angular - Testing
What is wrong with this test setup code?
beforeEach(() => {
  TestBed.configureTestingModule({
    declarations: [MyComponent]
  });
  fixture = TestBed.createComponent(MyComponent);
  component = fixture.componentInstance;
});
Afixture and component should be declared inside beforeEach
BShould import MyComponent instead of declaring it
CMissing call to compileComponents() before createComponent()
DNo need to call createComponent() in beforeEach
Step-by-Step Solution
Solution:
  1. Step 1: Check TestBed setup sequence

    When using TestBed with components, compileComponents() must be called to compile templates before creating the component.
  2. Step 2: Identify missing step

    The code configures the module but skips compileComponents(), which can cause errors or incomplete setup.
  3. Final Answer:

    Missing call to compileComponents() before createComponent() -> Option C
  4. Quick Check:

    compileComponents() needed before createComponent() [OK]
Quick Trick: Always call compileComponents() before createComponent() [OK]
Common Mistakes:
  • Skipping compileComponents() causes template errors
  • Declaring variables inside beforeEach unnecessarily
  • Thinking createComponent() is optional

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes