How to unit test the void method

I am trying to achieve more code coverage. I have an “informational” method that just triggers a notification and no response is required. How to do unit test?

public error(message?: any, ...optionalParams: any[]) { if (this.isErrorEnabled()) { console.error(`${this.name}: ${message}`, ...optionalParams); } } 
+6
source share
1 answer

You can check your side effects using spies , for example:

 describe('error method', => { it('should log an error on the console', () => { spyOn(console, 'error'); error(...); expect(console.error).toHaveBeenCalledWith(...); }); ... }); 
+8
source

Source: https://habr.com/ru/post/1012341/


All Articles