2016-11-15 102 views
6

我正試圖達到更多的代碼覆蓋率。我有一個「信息」方法,只是觸發通知,並不需要響應。我如何進行單元測試?如何單元測試無效方法

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

http://stackoverflow.com/questions/36402812/void-method-testing-javascript –

回答

6

可以使用spies測試它的副作用,例如:

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

     error(...); 

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

    ... 
}); 
+0

感謝的可能的複製很多,這個伎倆。 – user3506588

+0

幫了我很多,謝謝! –