Bird
0
0

Which of the following is the correct way to inject HttpTestingController in an Angular test?

easy📝 Syntax Q12 of 15
Angular - Testing
Which of the following is the correct way to inject HttpTestingController in an Angular test?
Aconst httpMock = inject(HttpTestingController, TestBed);
Bconst httpMock = new HttpTestingController();
Cconst httpMock = HttpClientTestingModule.get(HttpTestingController);
Dconst httpMock = TestBed.inject(HttpTestingController);
Step-by-Step Solution
Solution:
  1. Step 1: Recall Angular TestBed injection syntax

    Use TestBed.inject() to get service instances in tests.
  2. Step 2: Check each option

    Only const httpMock = TestBed.inject(HttpTestingController); uses correct syntax: TestBed.inject(HttpTestingController).
  3. Final Answer:

    const httpMock = TestBed.inject(HttpTestingController); -> Option D
  4. Quick Check:

    Use TestBed.inject() for services in tests = D [OK]
Quick Trick: Use TestBed.inject() to get HttpTestingController instance [OK]
Common Mistakes:
  • Trying to instantiate HttpTestingController with new
  • Using incorrect module methods
  • Passing wrong parameters to inject

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes