How to unit test a void method
10,141
You can test its 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(...);
});
...
});
Related videos on Youtube
Author by
Bahodir
Updated on June 04, 2022Comments
-
Bahodir almost 2 years
I am trying to reach more code coverage. I have an "informational" method that just triggers notification, and response is not required. How do I unit test it?
public error(message?: any, ...optionalParams: any[]) { if (this.isErrorEnabled()) { console.error(`${this.name}: ${message}`, ...optionalParams); } }