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

115
Views
Cómo probar la suscripción de un servicio a un sujeto de otro servicio

Dispongo de los siguientes servicios:

 @Injectable({ providedIn: 'root' }) export class Service1 { dataHasChanged = new Subject(); private data1; private data2; constructor() {} getData() { return { data1: this.data1, data2: this.data2 } } setData(data1, data2) { this.data1 = data1; this.data2 = data2; this.dataHasChanged.next({ data1: this.data1, data2: this.data2; }) } }
 @Injectable({ providedIn: 'root' }) export class Service2 { constructor(private service1: Service1) {} fetchClient(): Observable<ITest> { const data = service1.getData(); return this.http.post( '/api-endpoint/123', { data: data } ); } reload(): Observable<ITest> { return this.service1.dataHasChanged.pipe( mergeMap(() => this.fetchClient()), ); } }

Quiero probar que cada vez que se activa dataHasChanged, un componente que tiene service2.reload() recibe datos de su suscripción.

He creado la siguiente prueba para ello:

 describe('Service2', () => { let service2: Service2; let httpMock: HttpTestingController; let service1: Service1; let service1Stub: Partial<Service1>; beforeEach(() => { service1Stub = { dataHasChanged: new Subject(), getData: function () { return { data1: 123, data2: 12345 } } }; TestBed.configureTestingModule({ imports: [HttpClientTestingModule], providers: [ Service2, { provide: Service1, useValue: Service1Stub } ] }); service2 = TestBed.inject(service2); httpMock = TestBed.inject(HttpTestingController); service1 = TestBed.inject(service1); }); it('test', (done: DoneFn) => { service2.reload().subscribe((data) => { console.log('here') expect(data).toBeDefined(); done(); }); service1.dataHasChanged.next() }) });

El problema es que la prueba que está en la devolución de llamada de suscripción nunca se ejecuta ... ¿Cuál es la forma correcta de escribir esa prueba?

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

0

Descubrí que me estaba olvidando de simular el método fetchClient que tiene métodos http.

Esto funcionó:

 it('test', (done: DoneFn) => { spyOn(service2, 'fetchData').and.returnValue(of{ data1: 123, data2: 123 }) service2.reload().subscribe((data) => { console.log('here') expect(data).toBeDefined(); done(); }); service1.dataHasChanged.next() })
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!