mardi 9 janvier 2018

spyOn service / component Angular2

Hi guys trying to implement unit test with service. I'd like some clarification on why this isn't working.

setUp for my spec class.

beforeEach(() => {
    fixture = TestBed.createComponent(testComp);
    service = fixture.debugElement.injector.get(TestService);
});

 it('mockService', () => {
    spyOn(service, "testFuncCall");
    let buttonClick = fixture.debugElement.query(By.css('.testFuncCall'));
    buttonClick.triggerEventHandler('click', null);
    expect(service.testFuncCall).toHaveBeenCalled();
  })

So the above runs fine and if I make the button click - click on a different button it will fail. What I was trying to do was

 it('mockService', () => {
    spyOn(service, "testFuncCall");
    spyOn(component, "testFuncCall");
    let buttonClick = fixture.debugElement.query(By.css('.testFuncCall'));
    buttonClick.triggerEventHandler('click', null);
    expect(component.testFuncCall).toHaveBeenCalled();
    expect(service.testFuncCall).toHaveBeenCalled();
  })

This throws an error saying Expected spy testFuncCall to have been called. Just wondering why this happens. The component has a method called testFuncCall that gets initiated the button click . That method calls the Service which has a method with the same name testFuncCall.

If I have seperate it, one for testing if the component.testFuncCall has been called and another for if the service.testFuncCall has been called , it seems fine. But combining those into one throws error?

Aucun commentaire:

Enregistrer un commentaire