【问题标题】:How to unit test a void method如何对 void 方法进行单元测试
【发布时间】:2016-11-15 13:52:33
【问题描述】:

我正在尝试达到更多的代码覆盖率。我有一个只触发通知的“信息”方法,不需要响应。如何对它进行单元测试?

public error(message?: any, ...optionalParams: any[]) {
    if (this.isErrorEnabled()) {
      console.error(`${this.name}: ${message}`, ...optionalParams);
    }
  }

【问题讨论】:

标签: angular jasmine karma-jasmine


【解决方案1】:

您可以使用spies 测试它的副作用,例如:

describe('error method', => {
    it('should log an error on the console', () => {
        spyOn(console, 'error');

        error(...);

        expect(console.error).toHaveBeenCalledWith(...);
    });

    ...
});

【讨论】:

    猜你喜欢
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多