1

I start to learn Angular testing recently and faced with a misunderstanding of the UNIT testing concept. For example, I have a dialog with with 2 buttons: Confirm delete and Cancel. When user click Confirm Delete button the http-service method is called and dialog closes. Sounds simple. Here is the code:

confirmDelete() {
   this.httpService.delete(item)
       .subscribe(() => this.dialogRef.close(),
                  (err) => this.message = 'Some text');
} 

And my test for this function is:

it('should call service delete method and close the dialog', async( () => {
   spyOn(dialogRef, 'close'); // I have mockDialogRef;
   component.confirmDelete();
   fixture.whenStable().then(() => {
      fixture.detectChanges();
      expect(deleteSpy.calls.any().toBe(true)); // Spy on delete method
      expect(dialogRef.close).toHaveBeenCalled();
   });
}));

And it worked fine. Seems to be the delete method has been called and dialog has been closed. But my question is. What we should test in such methods? Should we always test the stuff inside subscribe method or only that service methods is called? And should I Test here the case if server return error? So, ..sorry for stupid question, but I'm really confused. Hope you guys will help me(

Natalya1
  • 35
  • 1
  • 4
  • I will try to test the code which part of the component which is under test. In this case, I will check * If httpService.delete was called with appropriate object (httpService should be mocked) * If the httpService returned success, then check if the dialog close is called. * If the httpService failed, then message was set. – Nikhil Walvekar Apr 26 '20 at 11:13

0 Answers0