how can we unit test httpResource in Angular?

Discussão em 'Angular' iniciado por SolidShook, Março 19, 2025.

  1. SolidShook

    SolidShook Guest

    I'm enjoying Angular's new HttpResource, it does a lot of stuff that I've been wanting for ages. It can really make life easier.

    However, I'm having trouble unit testing a httpResource with jasmine.

    For example, this does not work:

    fit('should call the expected URL', () => {
    TestBed.runInInjectionContext(() => {
    const resource = service.fetchSettings(); //httpResourceRef
    resource.reload();
    const req = httpMock.expectOne(`${environment.baseUrls.api.wellcomm}/1.0/cluster/getPendingSettings`); //HttpTestingController
    expect(req.request.method).toBe('GET');
    req.flush({});
    });
    });


    I would expect this to trigger the request method, so that the test passes. However, it does not.

    I know it's tied to how a component reads it, but how can I emulate this? Would I have to build a component that reads and renders the data every time I use this?

    Continue reading...

Compartilhe esta Página