There is a class called MyLogger with a static method getInstance(id: string) that returns an instance of Logger.
class Logger {
error(message: string): Logger {
// implementation...
}
}
In the system under test, there is a class Sut with a log property initialized with MyLogger.getInstance(). It has a testMethod() that calls the error() method on the logger.
class Sut {
log = MyLogger.getInstance();
testMethod() {
this.log.error('Foo');
}
}
The goal is to intercept the method call and pass a mock logger instead. One way to achieve this is by using a Mock object:
const loggerMock = new Mock<MyLogger>();
loggerMock
.setupStatic(()=>MyLogger.getInstance())
.returns(loggerMock.object());
// assert call to `error()` method has parameter 'Foo'