Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
¿Cómo ejecuto un caso de prueba en Angular 6 Jasmine-Karma en un método que llama a subscribe y devuelve datos de una API?

Este es mi archivo ts, donde llamo al método desde ngOnInit, y getAllAccounts es donde busco la API

 ngOnInit(): void { this.getAllAccounts(); } getAllAccounts() { this.service.getAllAccounts().subscribe({ next: (data: any) => { if (data) { this.allAccounts = data; } }, }); }

Este es mi archivo de especificaciones en el que intento probar los datos que se procesan

 it("should test subscribe method is getting called", fakeAsync(() => { const accountSpy = spyOn(service, "getAllAccounts").and.returnValue(of(getAllAccountsList())); const subSpy = spyOn(service.getAllAccounts(), "subscribe"); component.ngOnInit(); tick(100); expect(accountSpy).toHaveBeenCalled(); tick(300); expect(subSpy).toHaveBeenCalled(); })); it("should test execution within subscribe method", fakeAsync(() => { component.ngOnInit(); expect(component.allAccounts).toBeDefined(); expect(component.allAccounts.length).toBeGreaterThan(5); }));

Además, ¿cómo pruebo ngOnInit()?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El primer fixture.detectChanges() al que llama es cuando se llama a ngOnInit . Dicho esto, no hay problema en llamar a ngOnInit manualmente en una prueba unitaria. Para probar lo que tienes, yo haría:

 it('should set allAccounts', () => { // spy on the service method and return an observable spyOn(service, "getAllAccounts").and.returnValue(of(getAllAccountsList())); component.ngOnInit(); // assert that their lengths are the same expect(component.allAccounts.length).toBe(getAllAcountsList().length); });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!