A fake logger is a lightweight test double that replaces a real logger and records messages in accessible state for later assertions. It avoids writing to real destinations while preserving the logger’s expected method, such as info. section 4.7.1 section 4.7.2
A fake logger is useful because it:
- Receives the logger dependency through constructor injection instead of forcing production code to use a real logger. section 4.7.1
- Stores the supplied message in a public field, allowing a test to assert what was logged. section 4.7.1 section 4.7.2
- Can be reused as a stub or mock object, so the broader name fake avoids tying the class to one test role. section 4.7.1
- Can implement an ILogger interface when production code is designed for a replaceable logger rather than a hard dependency on SimpleLogger. section 4.7.2
Checking a recorded message
Create a FakeLogger, inject it into PasswordVerifier, run verify, and assert that mockLog.logged matches PASSED. The fake records the interaction without requiring the real logging destination. section 4.7.1
Listing 4.11 Class-based constructor injection
class PasswordVerifier {
_rules;
_logger;
constructor(rules, logger) {
this._rules = rules;
this._logger = logger;
}
verify(input) {
const failed = this._rules
.map(rule => rule(input))
.filter(result => result === false);
if (failed.length === 0) {
this._logger.info('PASSED');
return true;
}
this._logger.info('FAIL');
return false;
}
}
See the production shape: PasswordVerifier receives rules and a logger through its constructor, then calls info with PASSED or FAIL. section 4.7.1
