Skip to content
Back to skills

Angular Testing

ASecurity

Use Angular's HTTP testing backend rather than replacing `HttpClient` with a hand-written mock. `provideHttpClientTesting()` installs an in-memory backend and exposes `HttpTestingController`, which lets the test assert requests and decide when they complete. Provide `provideHttpClient()` first, then `provideHttpClientTesting()` so the testing backend can override the transport. For example, given a service like this: ```ts import { HttpClient } from '@angular/common/http'; import { Injectable...

  • 584 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added September 5, 2026
developmentgoangulartestingapibackend

Works with

  • cli
  • api

Security analysis

A100/100

Pro scans all 18 files and shows the line behind each finding

Scanned September 5, 2026

npx -y skills add HoangNguyen0403/agent-skills-standard --skill angular-testing --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Angular Testing?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Angular Testing
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hoangnguyen0403-angular-testing-06731ab4/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-angular-testing-06731ab4)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
# Mocking `HttpClient` in Angular tests

Use Angular's HTTP testing backend rather than replacing `HttpClient` with a hand-written mock. `provideHttpClientTesting()` installs an in-memory backend and exposes `HttpTestingController`, which lets the test assert requests and decide when they complete. Provide `provideHttpClient()` first, then `provideHttpClientTesting()` so the testing backend can override the transport.

For example, given a service like this:

```ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

type User = { id: string; name: string };

@Injectable({ providedIn: 'root' })
export class UserApi {
  constructor(private readonly http: HttpClient) {}

  getUser(id: string) {
    return this.http.get<User>(`/api/users/${id}`);
  }
}
```

the test can be written as:

```ts
import { TestBed } from '@angular/core/testing';
import { provideHttpClient } from '@angular/common/http';
import {
  HttpTestingController,
  provideHttpClientTesting,
} from '@angular/common/http/testing';
import { UserApi } from './user-api';

describe('UserApi', () => {
  let api: UserApi;
  let http: HttpTestingController;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        UserApi,
        provideHttpClient(),
        provideHttpClientTesting(),
      ],
    });

    api = TestBed.inject(UserApi);
    http = TestBed.inject(HttpTestingController);
  });

  afterEach(() => {
    http.verify();
  });

  it('requests and returns a user', () => {
    let received: User | undefined;
    api.getUser('42').subscribe(user => (received = user));

    const request = http.expectOne({
      method: 'GET',
      url: '/api/users/42',
    });
    request.flush({ id: '42', name: 'Ada Lovelace' });

    expect(received).toEqual({ id: '42', name: 'Ada Lovelace' });
  });

  it('exposes an HTTP error to the subscriber', () => {
    let status: number | undefined;
    api.getUser('missing').subscribe({
      next: () => fail('expected an error'),
      error: error => (status = error.status),
    });

    const request = http.expectOne('/api/users/missing');
    request.flush('Not found', { status: 404, statusText: 'Not Found' });

    expect(status).toBe(404);
  });
});
```

Subscribe before calling `expectOne()`, because an HTTP observable normally creates its request on subscription. Use a `RequestMatch` object or predicate to assert query parameters, headers, or a request body through `request.request`. Use `flush()` for a normal response and `request.error()` for a network-style failure. `http.verify()` in `afterEach()` catches requests that a test forgot to handle.

Older module-based projects can use `HttpClientTestingModule`, but do not mix that module-based setup with the provider-based setup in one test. Match the approach to the Angular version and the rest of the application configuration.

Files in this skill

  • eval-1.baseline.md2.6 KB
  • eval-1.with-skill.md2.7 KB
  • eval-2.baseline.md2.9 KB
  • eval-2.with-skill.md3.1 KB
  • eval-3.baseline.md2.6 KB
  • eval-3.with-skill.md2.8 KB
  • eval-4.baseline.md2.9 KB
  • eval-4.with-skill.md3.3 KB
  • eval-5.baseline.md3.1 KB
  • eval-5.with-skill.md3.4 KB
  • eval-6.baseline.md2.9 KB
  • eval-6.with-skill.md2.7 KB
  • trigger-1.md124 B
  • trigger-2.md143 B
  • trigger-3.md136 B
  • trigger-4.md132 B
  • trigger-5.md143 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.