Bahodir
Bahodir

Reputation: 539

How to unit test a void method

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);
    }
  }

Upvotes: 12

Views: 12272

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 122157

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(...);
    });

    ...
});

Upvotes: 14

Related Questions