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.

4
  • Have you tried calling ngOnChanges? Commented Sep 14, 2018 at 13:24
  • Is this a spied function? divActualPrice.componentInstance.modifyTotalAmount Commented Sep 14, 2018 at 13:47
  • 1
    You should be setting the setPrice on line 2 not price property Commented Sep 14, 2018 at 14:07
  • You should do some more reading on how get and set works. I thinks you may have a misunderstanding. You are trying to call the property price on the instance. Why do you think this would cause the setPrice setter to be called? Commented Sep 14, 2018 at 18:00

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.