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

152
Views
Burlarse de la clase base en una prueba unitaria para Angular 2

Estoy tratando de escribir una prueba unitaria para ver si se llama al método de clase base

Aquí está la clase base

 export abstract class Animal{ protected eatFood() { console.log("EAT FOOD!") } }

Esta es la clase que quiero probar.

 export class Monkey extends Animal { onHungry(){ this.eatFood(); } }

aquí está la prueba

 class MockAnimal { public eatFood() { console.log("EAT MOCKED FOOD!"); } } describe('Monkey', () => { beforeEach(() => { TestBed.configureTestingModule({ declarations:[Monkey], providers: [ { provide: Animal, useClass: MockAnimal } ] } }); it('eat food when hungry', fakeAsync(() => { let fixture = TestBed.createComponent(Monkey); spyOn(fixture, 'eatFood'); fixture.componentInstance.onHungry(); expect(fixture.eatFood).toHaveBeenCalled(); })); }

No puedo obtener la prueba unitaria para ejecutar esta clase MockAnimal . ¿Es así como probarlo?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

TestBed se usa para probar componentes y, como en un NgModule normal, define declaraciones, importaciones, proveedores, ... Pero lo que quiere probar es una clase simple. En la prueba tienes que inicializarlo manualmente:

 describe('Monkey', () => { it('eats if hungry', () => { const monkey = new Monkey(); // spying will only work if 'eatFood' is public const eatFoodSpy = spyOn(monkey, 'eatFood'); monkey.onHungry(); expect(eatFoodSpy).toHaveBeenCalled(); }); });
about 4 years ago · Santiago Trujillo Report

0

Puedes hacer esto, que se burlará de tu llamada.

 it('eat food when hungry', fakeAsync(() => { let fixture = TestBed.createComponent(Monkey); fixture.componentInstance['eatFood'] = jasmine.createSpy('eatFood'); fixture.componentInstance.onHungry(); expect(fixture.eatFood).toHaveBeenCalled(); }));
about 4 years ago · Santiago Trujillo 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!