PractitionerTest-driven practice · 3 of 3about 20 min

Test a class with state and errors

Write tests for the BankAccount class described in the contract. Five broken versions are waiting.

Check state through getBalance() after each action, check that invalid actions throw the documented message, and check that a failed action leaves the balance untouched.

Before this: Guide: TDD in Practice, Challenge: A cart total that survives the edge cases

Contract

class BankAccount {
  /** Starts with the given balance (default 0). */
  constructor(initialBalance = 0)

  /** Adds a positive amount. Throws Error("Amount must be positive") for 0 or less. */
  deposit(amount)

  /**
   * Removes a positive amount. Throws Error("Amount must be positive") for 0 or less,
   * and Error("Insufficient funds") when amount is more than the balance.
   * Withdrawing the whole balance is allowed.
   */
  withdraw(amount)

  /** Current balance. */
  getBalance()
}

Your tests