Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Angular: Unit testing GET method in service

I have a service method that returns item and unit test for the get method. However, the coverage shows that the getter method is not covered. Can you suggest what am I doing wrong in unit test?

export class ItemService{
#item: Item;
  get item(): Item{
    return this.#item;
  }
}
import itemMock from '../mocks/item-response.json';
describe('ItemService', () => {
  let itemService: ItemService;
  it('should get item information', () => {
      itemService['#item'] = itemMock ;
      spyOnProperty(itemService, 'item').and.returnValue(itemMock);
      expect(itemService['#item']).toEqual(itemMock );
  });
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Ok, so I think the problem is your spy is actually replacing the getter, and this one is never called.
Maybe you could change strategy and only check on the private property without Spy ?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading