Bird
0
0

In this test, why does httpMock.verify() cause the test to fail?

medium📝 Debug Q6 of 15
Angular - Testing
In this test, why does httpMock.verify() cause the test to fail?
service.getData().subscribe();
// forgot to call httpMock.expectOne and flush
httpMock.verify();
ABecause verify requires a flush call first
BBecause verify must be called before subscribe
CBecause verify only works with POST requests
DBecause there is an outstanding HTTP request not handled
Step-by-Step Solution
Solution:
  1. Step 1: Understand verify() purpose

    verify() checks that no HTTP requests are outstanding (unmatched or unflushed).
  2. Step 2: Identify missing expectOne and flush

    Without expectOne and flush, the request remains pending, causing verify() to fail.
  3. Final Answer:

    Because there is an outstanding HTTP request not handled -> Option D
  4. Quick Check:

    verify fails if requests remain unhandled [OK]
Quick Trick: Always expectOne and flush before verify() [OK]
Common Mistakes:
  • Calling verify before any HTTP calls
  • Assuming verify needs flush first regardless of requests
  • Thinking verify only applies to POST

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes