2

In my Component

@Input('price')
set setPrice(price) {
    this.price = price;
    this.modifyTotalAmount();
}

test (component.spec.ts)

it('should call function ', () => {
    spyOn(fixture.componentInstance, 'modifyTotalAmount');
    fixture.componentInstance.price = 4500;
    fixture.detectChanges();
    const divActualPrice = fixture.debugElement.query(By.css('#actualPrice'));
    expect(divActualPrice.componentInstance.modifyTotalAmount).toHaveBeenCalled();
});

Normally when parent compoent value get changed this setPrice(price) hits and modifyTotalAmount() function called. But when unit test runs that modifyTotalAmount() not called. this test case get faild. I think what I have done in test case could be wrong. can anyone please clarify whats wrong with this.

Ray
  • 3,864
  • 7
  • 24
  • 36
Sachithra Wishwamal
  • 117
  • 1
  • 2
  • 10

1 Answers1

3

As Aniket Kadam already pointed out, when you set fixture.componentInstance.price = 4500 you are not using the setter setPrice that you would need to call in order to trigger this.modifyTotalAmount()

So do fixture.componentInstance.setPrice = 4500 instead.

AlexG
  • 76
  • 2