return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>jasmine - how can we unit test httpResource in Angular? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jasmine - how can we unit test httpResource in Angular? - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Mar 27 at 20:17 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Mar 19 at 16:25 SolidShookSolidShook 491 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You should be using the HttpTestingController, as this was already the case for testing the HttpClient.

@Component({
  selector: 'app-my-component',
  template: `
  <h1>Name: {{ data.value()?.name }}
  </h1>`,
})
export class MyComponentComponent {
  data = httpResource(() => `https://swapi.dev/api/planets/1/`);
}

describe('...', () => {
  let fixture: ComponentFixture<MyComponentComponent>;
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        provideHttpClient(),
        provideHttpClientTesting(),
      ],
    });

    fixture = TestBed.createComponent(MyComponentComponent);
  });

  it('...', async () => {
    const httpMock: HttpTestingController = TestBed.inject(
      HttpTestingController
    );

    // This will trigger the http request
    TestBed.inject(ApplicationRef).tick();
    fixture.detectChanges();
 
    // expectation + flushing mock response
    httpMock
      .expectOne('https://swapi.dev/api/planets/1/')
      .flush({ name: 'Potato' });

    // Waiting for the app to stabilise = resource to resolve
    await TestBed.inject(ApplicationRef).whenStable();

    expect(fixtureponentInstance.data.status()).toBe(
      ResourceStatus.Resolved
    );

    // Render the template after the resource is resolved
    fixture.detectChanges();
    expect(fixture.nativeElement.querySelector('h1').textContent).toContain(
      'Name: Potato'
    );

    httpMock.verify();
  });
});
发布评论

评论列表(0)

  1. 暂无评论