Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
Mock class object for condition (instance of) to be true in unit testing angular

I need help in order to write unit tests for router.events.subscribe. I used ActivationEnd to use snapshot object which has properties which I need for my application. Below is my code snippet.

In 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;
      }
  }

Unit test File

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; }
}

and using MockRouter class in providers

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

Can you please help me how can I mock ActivationEnd event and pass it to onRouterEvent() so that if conditions evaluates to be true ?

Regards, Alpesh

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda