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

185
Views
Prueba angular 'suscribirse no es una función'

En mi componente dentro ngOnInit tengo un subscribe() dentro del cual llamo a una función de componente:

 ngOnInit(): void { this.navigationService.itemReset$.subscribe(event => this.resetItem(event)); }

en el servicio de navegación tengo:

 btnPressed$: Subject<Event> = new Subject(); itemReset$: Observable<Event> = this.btnPressed$.asObservable(); keyDown(event: Event): void { this.btnPressed$.next(event); }

Busqué mucho sobre este problema e intenté algo como esto en mi prueba de componentes:

 describe('MyComponent', () => { let component: MyComponent; let fixture: ComponentFixture<MyComponent>; let navigationServiceMock = { itemReset$: () => of({} as Event), // tried this: itemReset$: jasmine.createSpy('itemReset$').and.returnValue(of({})), }; beforeEach(async () => { await TestBed.configureTestingModule({ declarations: [MyComponent], providers: [{ provide: navigationService, useValue: navigationServiceMock }] }) .compileComponents(); }); beforeEach(() => { fixture = TestBed.createComponent(MyComponent); component = fixture.componentInstance; fixture.detectChanges(); }); it('should create', () => { expect(component).toBeTruthy(); }); });

pero aún así, obtener el error

 TypeError: this.navigationService.itemReset$.subscribe is not a function

¿Alguna idea, por qué no funciona? Estaría muy agradecido por cualquier ayuda!

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

0

Asegúrese de que el tipo de itemReset$ en su objeto falso coincida con el tipo en la implementación.

Debe pasar un Observable<Event> pero tenía una función que devolvía tal observable en su lugar.

 let navigationServiceMock = { itemReset$: of({} as Event), };
about 4 years ago · Juan Pablo Isaza Report

0

Estás cerca pero recuerda que te estás burlando:

 let navigationServiceMock = { itemReset$: () => of({} as Event), // tried this: itemReset$: jasmine.createSpy('itemReset$').and.returnValue(of({})), };

Se ha burlado de una función llamada itemReset$ que devuelve un observable.

En realidad, desea que itemReset$ sea el observable.

Así que cambia tu simulacro a:

 let navigationServiceMock = { itemReset$: of({} as Event), };
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!