Created
March 21, 2025 18:38
-
-
Save trikitrok/24517ddee463b5799c8ec782df30cfd1 to your computer and use it in GitHub Desktop.
Alternative using Parameterize Method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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