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

178
Views
Objeto de clase simulado para que la condición (instancia de) sea verdadera en la prueba unitaria angular

Necesito ayuda para escribir pruebas unitarias para router.events.subscribe . ActivationEnd para usar un objeto de instantánea que tiene propiedades que necesito para mi aplicación. A continuación se muestra mi fragmento de código.

en constructor,

 this.router.events.subscribe(e => this.onRouterEvent(e))
 onRouterEvent(e: Event): void { if (e instanceof ActivationEnd) { this.section = e.snapshot.data.title; this.dynamicSubNavLabel = e.snapshot.routeConfig?.data?.label; } }

Archivo de prueba unitaria

 class ActivationEnd { snapshot: any = { data: { title: 'a title' }, routeConfig: { data: { label: 'a label' } }, params: { orderID: "3525", projectID: "24", usecaseID: "3" } }; } class MockRouter { events: Observable<Event> = new Observable(observer => { observer.next(new ActivationEnd()); // This does NOT make the condition true (e instance of ActivationEnd) observer.complete(); }); get url(): string { return 'asd'; } navigateByUrl(url: string) { return url; } navigate(path: string) { return path; } }

y usando la clase MockRouter en proveedores

 TestBed.configureTestingModule({ declarations: [AppComponent], imports: [ RouterTestingModule ], providers: [ { provide: Router, useClass: MockRouter }, ], schemas: [NO_ERRORS_SCHEMA] })

¿Pueden ayudarme, por favor, cómo puedo simular el evento ActivationEnd y pasarlo a onRouterEvent() para que las condiciones se evalúen como verdaderas?

Saludos, Alpesh

about 4 years ago · Juan Pablo Isaza
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!