44

After upgrading my application to Angular 18.0.4, my test classes say:

'HttpClientTestingModule' is deprecated. Add provideHttpClientTesting() to your providers instead.

Therefore I adapted my code as follows:

        await TestBed.configureTestingModule(
            {
                imports: [
                    AssetDetailsComponent,
                ],
                providers: [
                    // replacement for HttpClientTestingModule:
                    provideHttpClientTesting() 
                ]
            })
            .compileComponents();

However, when I run the tests, I get the following error:

NullInjectorError: R3InjectorError(Standalone[AssetDetailsComponent])[InventoryActionService -> InventoryActionService -> _HttpClient -> _HttpClient]:
      NullInjectorError: No provider for _HttpClient!

If I use provideHttpClient() instead of provideHttpClientTesting() it works, yet I doubt that this is best practice. What is the correct solution to this issue?

3 Answers 3

64

Also add provideHttpClient() before provideHttpClientTesting()

import { provideHttpClient } from '@angular/common/http'
import { provideHttpClientTesting } from '@angular/common/http/testing'

providers: [
   provideHttpClient(),
   provideHttpClientTesting() 
]

As mentioned in the docs.

Sign up to request clarification or add additional context in comments.

2 Comments

so we can still make a httpClientTestingController with this? let httpTestingController: HttpTestingController;
One of our test classes is able to utilize (get from the TestBed) a httpTestingController, simply by calling TestBed.inject(HttpTestingController), even when using provideHttpClient() and provideHttpClientTesting(). Not sure if that is exactly your use case though?
13

You need provideHttpClient() alsom Since provideHttpClientTesting() configures env but does not provide httpClient, Include provideHttpClient() like below:

import { TestBed } from '@angular/core/testing';
import { AssetDetailsComponent } from './asset-details.component';
import { provideHttpClient } from '@angular/common/http';
import { provideHttpClientTesting } from '@angular/common/http/testing';
   

await TestBed.configureTestingModule({
  imports: [
    AssetDetailsComponent,
  ],
  providers: [        
    provideHttpClient(), // Provide the HttpClient along with HttpClientTesting
    provideHttpClientTesting(),
  ]
}).compileComponents();

Comments

-1
import { TestBed } from '@angular/core/testing';
import { HttpClient } from '@angular/common/http';
import { provideHttpClient } from '@angular/common/http';
import { provideHttpClientTesting, HttpTestingController } from '@angular/common/http/testing';

interface User {
  id: number;
  name: string;
}

class UserService {
  constructor(private http: HttpClient) {}

  getUsers() {
    return this.http.get<User[]>('/api/users');
  }
}

describe('UserService', () => {
  let service: UserService;
  let httpTestingController: HttpTestingController;

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

    service = TestBed.inject(UserService);
    httpTestingController = TestBed.inject(HttpTestingController);
  });

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

  it('should fetch users', () => {
    const mockUsers: User[] = [
      { id: 1, name: 'John' },
      { id: 2, name: 'Jane' }
    ];

    service.getUsers().subscribe(users => {
      expect(users).toEqual(mockUsers);
    });

    const req = httpTestingController.expectOne('/api/users');
    expect(req.request.method).toEqual('GET');
    req.flush(mockUsers);
  });
});

Credit

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.