Sub topic 11
Catching stray requests with verify
This chapter assumes you can write a basic HttpTestingController test. You should know how to configure provideHttpClient() and provideHttpClientTesting(), match a request with expectOne, and answer it with flush. The examples use the UserService from earlier in this tutorial.
Why a passing test can still be wrong
expectOne only checks the request you asked about. It says nothing about other requests the service made, or about requests you matched but never answered. A test can pass while the service sends a duplicate call or leaves an observable waiting forever.
HttpTestingController.verify() closes that gap. It throws if any request is still open, meaning it was sent but never matched by expectOne, match, or expectNone.
The afterEach pattern
Call verify() in afterEach so it runs after every test in the suite.
import { TestBed } from '@angular/core/testing';
import { provideHttpClient } from '@angular/common/http';
import {
HttpTestingController,
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { UserService } from './user.service';
describe('UserService stray requests', () => {
let service: UserService;
let httpTesting: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [provideHttpClient(), provideHttpClientTesting()],
});
service = TestBed.inject(UserService);
httpTesting = TestBed.inject(HttpTestingController);
});
afterEach(() => {
httpTesting.verify();
});
it('fails when a second request is never handled', () => {
service.getUser(1).subscribe();
service.getUser(2).subscribe();
httpTesting.expectOne('/api/users/1').flush({ id: 1, name: 'Ada' });
// /api/users/2 is still open, so verify() in afterEach throws.
});
});The failure message lists the open requests, for example Expected no open requests, found 1: GET /api/users/2. That tells you which call the service made without the test knowing about it.
What verify counts
verify() fails on requests that are:
- Matched by nothing, because the test never called
expectOneormatchfor them. - Not matched because the URL, method, or query string differs from what you wrote.
A request you matched with expectOne but never flushed does not fail verify(). Matching removes the request from the open list. The subscriber simply never receives a value, and a test that asserts on the result will fail on its own assertion instead.
Asserting that nothing was sent
Use expectNone when the correct behavior is to send no request. A cache hit is a typical case.
it('does not send a request for an invalid id', () => {
service.getUser(1).subscribe();
httpTesting.expectNone('/api/users/2');
httpTesting.expectOne('/api/users/1').flush({ id: 1, name: 'Ada' });
});expectNone fails immediately if a request matches. Use it for a specific URL or predicate you want to rule out. Use verify() as the general net for everything else.
Cancelled requests
Unsubscribing from an HttpClient observable cancels the request. By default verify() still counts a cancelled request as open until you match it. If a test cancels requests on purpose, such as a switchMap that drops the previous call, tell verify() to skip them.
it('ignores a request that was cancelled', () => {
const subscription = service.getUser(1).subscribe();
subscription.unsubscribe();
httpTesting.verify({ ignoreCancelled: true });
});Use ignoreCancelled only where cancellation is the behavior under test. Setting it globally hides real leaks.
Common mistakes
- Calling
verify()mid-test. Requests that are legitimately waiting for later steps will trigger the error. Keep it inafterEach, or call it at the end of a test. - Forgetting that
TestBedis reset per test. Each test gets a fresh controller, so open requests from one test never leak into the next. Ifverify()complains, the stray request belongs to the current test. - Matching by a loose predicate. A function matcher that accepts too much can hide a duplicate request. Prefer an exact URL or a
{ method, url }object.
Exercise
A test calls service.getUser(1).subscribe(), matches the request with httpTesting.expectOne('/api/users/1'), and never calls flush. afterEach calls httpTesting.verify(). Does verify() fail, and why or why not?
Show answer
No. expectOne removes the request from the open list, so verify() sees no outstanding requests. The subscriber never gets a response, so only an assertion on the result, or an expectation that the callback ran, would catch the missing flush.