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

196
Vistas
Unit testing monkey patch in Angular (Cannot read properties of undefined (reading 'checkABC'))

I'm attempting to unit test my setStep() method in angular using Jasmine but I keep getting Cannot read properties of undefined (reading 'checkBusiness') thrown which prevents me from continuing the test.

This is the function in the component.ts:


  get userBusiness(): string { return  this.auth &&  this.auth.user && this.auth.user.Source; }

setSteps() {
    if (this.userBusiness.checkBusiness('ABC')) {
      this.items = steps['ABC'];
      if (this.application.process.onboarding.status !== 'complete') { this.show = true; }
    } else {
      this.items = steps['DEF'];
    }
  }

checkBusiness() method is defined as a monkey patch to the String prototype class:

Interface String {
  checkBusiness(...args: string[]): boolean;
}

String.prototype.checkBusiness = function(...args: string[]): boolean {
  let result = false;
  for (let arg of args) {
    const currentBusinessArray = business[this];
    if (!currentBusinessArray) {
      result = false;
      continue;
    }

    if (currentBusinessArray.includes(arg)) {
      result = true;
    }
  }
  return result;
};

Business is an array defined as:

const lob = Object.freeze( <any> {
  ABC : ['ABC', 'ABC1', 'ABC2', 'ABC3'],
  DEF : ['DEF', 'DEF1', 'DEF2', 'DEF3']
});

Testing/spec file:

  it('should confirm that "show" is set to true in setSteps()', () => {
    const testUserBusiness = {...testDataSvc.userABC}
    const userBusinessSpy = spyOnProperty(component, 'userBusiness').and.returnValue(testUserBusiness);
    console.log(component['userBusiness']);
    component.setSteps();
})

Problem:: I keep getting Cannot read properties of undefined (reading 'checkBusiness') thrown

Any idea how to get past this? I've tried to search everywhere but there isn't something like this scenerio that I can follow.

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