Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Created March 21, 2025 18:38
Show Gist options
  • Save trikitrok/24517ddee463b5799c8ec782df30cfd1 to your computer and use it in GitHub Desktop.
Save trikitrok/24517ddee463b5799c8ec782df30cfd1 to your computer and use it in GitHub Desktop.
Alternative using Parameterize Method
// Alternative using Parameterize Method
export class User {
private id: number;
constructor(id: number) {
this.id = id;
}
// more code...
//!! we used Parameterize Method to pass a BankingServices instance to the method
public updateBalance(amount: Money, bankingServices: BankingServices): void {
bankingServices.updateBalance(this.id, amount);
}
// more code...
}
//!! In some test now we can use a test double of BankingServices to sense or separate
describe("User", () => {
it('updates balance', () => {
const bankingServices: jest.Mocked<BankingServices> = { updateBalance: jest.fn() };
const userId = 1;
const user = new User(userId);
const amount = new Money(200);
user.updateBalance(amount, bankingServices);
expect(bankingServices.updateBalance).toHaveBeenCalledWith(userId, amount); // in this case sensing
});
// more tests...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment