Notes on TDD - chapter 4

Notes on TDD - chapter 4

February 11, 2024 | tdd, python

This chapter is straightforward. The problem is that our amount is public. Anyone can change, and we don’t want to allow tests to access it. What happens if we delete that variable and move it somewhere else? We will have to update the test. Lets refactor it.

class DollarV5:
    _amount: int

    def __init__(self, amount: int):
        self._amount = amount

    def times(self, multipler: int) -> "DollarV5":
        return DollarV5(self._amount * multipler)

    def __eq__(self, value: "DollarV5") -> bool:
        return self._amount == value.amount

We should keep __eq__ as is. Yes, this will break our next test, but its fine for now.

def test_times():
    five = DollarV5(5)
    assert five.times(2) == DollarV5(10)


Aaaaaand it broke. It says AttributeError because it could not find amount attribute when making the comparison against the value passed.

No notes link to this note

Go to random page