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

436
Vistas
Jasmine Unit Test Abstract class

Is there a way to create a jasmine unit test for an abstract component?

doing

const fixture = TestBed.createComponent(MyAbstractComponent);

says, "cannot assign an abstract constructor type to a non-abstract constructor type"

I tried some searching but nothing comes up.

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

0

You can create a simple class in your test file which extends from abstract class (do not forget to mock abstract methods), than just test its non abstract methods. Let's say we have MyAbstractClass:

export abstract class MyAbstractClass {
  sum(a: number, b: number): number {
    return a + b;
  }

  abstract calc1(): void;
  abstract calc2(): void;
}

and then in spec file we can just create a new derived class:

class MyClass extends MyAbstractClass {
  // just mock any abstract method
  calc1(): void {
    return;
  }
  calc2(): void {
    return;
  }
}

So now we can write tests for non-abstract methods:

describe('MyAbstractClass', () => {
  let myClass: MyClass;
  beforeEach(() => {
    myClass = new MyClass();
  });

  it('sum two args', () => {
    const a = 1, b = 2;

    const sum = myClass.sum(a, b);

    expect(sum).toBe(3);
  });
});

Also created a stackblitz example of this test example.

about 4 years ago · Santiago Trujillo Denunciar

0

Here is what I did to test an angular pipe SafePipe that was using a built in abstract class DomSanitizer.

// 1. Import the pipe/component to be tested
import { SafePipe } from './safe.pipe';
// 2. Import the abstract class
import { DomSanitizer } from '@angular/platform-browser';

// 3. Important step - create a mock class which extends 
// from the base class imported above and add implement 
// the mock methods
class MockDomSanitizer extends DomSanitizer {
  sanitize(): string{
    return 'ANY';
  }
  bypassSecurityTrustHtml(): string{
    return 'ANY'
  }
  bypassSecurityTrustStyle(): string{
    return 'ANY'
  }
  bypassSecurityTrustScript(): string{
    return 'ANY'
  }
  bypassSecurityTrustUrl(): string{
    return 'ANY'
  }
  bypassSecurityTrustResourceUrl(): string{
    return 'ANY'
  }
}

describe('SafePipe', () => {

  it('should return an HTML string', () => {
    // 4. create an instance of pipe class and inject the mocked class above
    let safePipe = new SafePipe(new MockDomSanitizer());

    // 5. Add your asserions as ususal
    expect(safePipe.transform(null, 'html')).toBeTruthy();
    expect(safePipe.transform(null, 'style')).toBeTruthy();
    expect(safePipe.transform(null, 'script')).toBeTruthy();
    expect(safePipe.transform(null, 'url')).toBeTruthy();
    expect(safePipe.transform(null, 'resourceUrl')).toBeTruthy();
    expect(safePipe.transform(null, 'anything')).toContain('anything');
  });

});

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