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

121
Views
Mocking base class in a unit test for Angular 2

I'm trying to write a unit test to see if the base class method gets called

Here is the base class

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

Here is the class I want to test

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

Here is the test

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();
  }));
}

I can't get the unit test to run this MockAnimal class. Is this how to test it?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The TestBed is used to test components and as in a normal NgModule you define declarations, imports, providers, ... But what you want to test is a simple class. In the test you have to initialize it manually:

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

You can do this, that will mock your call.

  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!