Part two of my Test-Driven Development in Unity in series, where we’ll be implementing a heart based health system much like the one found in the Zelda franchise.
In part 2 we round out the Heart class by implementing the Deplete method. The Deplete method works much like Replenish excepts it lowers Heart’s UI Image fill amount by 25% rather than incrementing it.
To polish off the Heart class’s implementation, we improve our API by throwing an ArgumentOutOfRange exception for negative values. This will let future developers know that negative values are outside the allowable range of values, keeping the use of our API clear and concise.
After moving the Heart class into it’s own file, we finish of part 2 by creating a throwaway class that will serve as a vehicle for seeing our code in action. This is an optional step, but one that I find to be very gratifying.
- Implemented Heart’s Deplete method
- Removed instantiation duplication from our tests
- Improved our API by throwing an exception for negative input values
- Moved the Heart class into its own file
- Created a throwaway MonoBehaviour to visualize our progress
Lessons & Takeaways
- Non-compiling tests are valid for driving the addition of production code
- Look up and understand suggestions made by your IDE and other productivity tools
- Always make sure your tests are passing BEFORE you refactor
- Don’t compromise the usability of API by over-optimizing or over-engineering
Links & Resources
Thanks for watching! Please feel free to like, comment, and subscribe