Mocking base class unit test angular 2

I am trying to write a unit test to see if a base class method is called

Here is the base class

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

      

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 cannot get a unit test to run this MockAnimal class. Is this the best way to test it?

Sorry if this is a noob question, I am just starting out with angular 2

Any help would be appreciated.

thank

+3


source to share





All Articles