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

225
Vistas
TypeError karma test : subscribe is not a function

I have a karma unit test and the test fails with below error.

this.gridApi.getScaleWidth().subscribe is not a function

GridApi.ts

export class GridApi {

    private scaleWidthSubject = new BehaviorSubject<{value: number}>({value: 0});

    public getScaleWidth(): Observable<{value:number}> {
        return this.scaleWidthSubject;
    }
}

GridComponent.ts

export class GridComponent implements OnInit, OnDestroy, AfterViewInit {

    private subscribeToValueChanges() {        
        this.scaleWidth$ = this.gridApi.getScaleWidth().subscribe( width => {
        this.scaleWidth = width.value;
    });

 }
}

Component.spec.ts

describe('GridComponent', () => {

beforeEach(async () => {
    const mockGridApiService = jasmine.createSpyObj("GridApi", {
        getScaleWidth () : Observable<{value: number}> {
            let scaleWidthSubject = new BehaviorSubject<{value: number}>({value: 0});
            return scaleWidthSubject.asObservable();
        }
    });
}

await TestBed.configureTestingModule({
    providers: [ { provide: GridApi, useValue: mockGridApiService} ],
    imports: [
        HttpClientModule
    ],
    declarations: [ GridComponent ]

})
}

What should the mock getScaleWidth() return to pass the test. Not sure what I'm missing here.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

describe('GridComponent', () => {
  const mockGridService = jasmine.createSpyObj<GridApi>('GridApi', ['getScaleWidth'])

  beforeEach(() => {
    mockGridService.getScaleWidth.and.returnValue(of({ value: 0 }));
  });

  await TestBed.configureTestingModule({
    providers: [ { provide: GridApi, useValue: mockGridService} ],
    imports: [HttpClientModule],
    declarations: [ GridComponent ]
  })

  it('should call getScaleWidth from service', () => {
    // the component function that triggers the service call is private 
    // make the call from component
  
    expect(mockGridService.getScaleWidth).toHaveBeenCalled(); 
    mockGridService.getScaleWidth().subscribe(response => {
       expect(response.value === 0)
    }) 
  })
}
about 4 years ago · Santiago Trujillo Denunciar
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