'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?



Solution 1:[1]

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

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Alex Hora