範例測試:saveMoney
方法
atm.js
:
class ATM { constructor(balance) { this.balance = balance; } saveMoney(amount) { if (amount <= 0) { throw new Error("Amount must be positive"); } this.balance += amount; return this.balance; } withdrawMoney(amount) { if (amount <= 0) { throw new Error("Amount must be positive"); } if (amount > this.balance) { throw new Error("Insufficient balance"); } this.balance -= amount; return this.balance; } getBalance() { return this.balance; } reset() { this.balance = 0; } setBalance(balance) { if (balance < 0) { throw new Error("Balance cannot be negative"); } this.balance = balance; } }
export default ATM;
|
atm.test.js
:
import ATM from "../atm.js";
test("saveMoney adds money to balance", () => { const atm = new ATM(0);
atm.saveMoney(1000);
expect(atm.balance).toBe(1000); });
|
逐步對應:
步驟 |
內容 |
Arrange |
建立 ATM 物件並給初始值 |
Act |
呼叫 saveMoney(1000) |
Assert |
驗證 atm.balance 是否等於 1000 |
另一個範例:檢查錯誤拋出
如果要測試當金額是負數時會丟錯誤:
test("saveMoney throws error when amount <= 0", () => { const atm = new ATM(0);
expect(() => atm.saveMoney(0)).toThrow("Amount must be positive"); });
|
這裡因為 act
跟 assert
綁在一起,所以在 expect
裡包了一個 function,來驗證是否拋出錯誤。
完整測試檔(用 AAA 標註)
import ATM from "../atm.js";
test("saveMoney adds money to balance", () => { const atm = new ATM(0);
atm.saveMoney(1000);
expect(atm.balance).toBe(1000); });
test("saveMoney throws error when amount <= 0", () => { const atm = new ATM(0);
expect(() => atm.saveMoney(0)).toThrow("Amount must be positive"); });
|
Comments