Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Last active March 20, 2025 23:11
Show Gist options
  • Save trikitrok/2bc194f1f8c0b50cfbbd60f3d4833098 to your computer and use it in GitHub Desktop.
Save trikitrok/2bc194f1f8c0b50cfbbd60f3d4833098 to your computer and use it in GitHub Desktop.
class Library {
private display: Display;
constructor(display: Display) {
this.display = display;
}
printBooks(books: Book[]): void {
const inputReader = new ConsoleInputReader(); // creating the ConsoleInputReader here impedes unit testing
const libraryName = inputReader.readLine();
this.display.show(libraryName);
for (const book of books) {
this.display.show(book.getName());
}
}
}
//////////////////////////////////////////////
// After parameterizing the method
class Library {
private display: Display;
constructor(display: Display) {
this.display = display;
}
// !! Method was parameterized and now we can test it
// passing a test double that overrides `readLine(): string;`
printBooks(books: Book[], inputReader: ConsoleInputReader): void {
const libraryName = inputReader.readLine();
this.display.show(libraryName);
for (const book of books) {
this.display.show(book.getName());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment