Try writing your test first, before production code.
If you're building a feature or fixing a bug that will affect external behaviour - start with an integration test at the crate level.
Try thinking what would be the ideal code you would like to interact with, what would be the types, what would be the functions ? A broad-strokes scenario, not caring much about implementation details, not caring much about covering all edge cases. Write that code. It does not compile. But it looks nice, you're pleased.
Read through again, add assertions. Add the types. For each missing feature or a bug that is present in this high level scenario - write a unit test. Satisfy that test with changes to production code. Maybe refactor a bit in between. Once the big test is green - you're done !
There is no Rust-focused TDD book just yet for me to recommend, but here, have some for other languages:
- Kent Beck - Test Driven Development: By Example
- Steve Freeman, Nat Pryce - Growing Object-Oriented Software, Guided by Tests
Rust allows for more cool tricks and generally writing less test code than mentioned in these books, so please use your judgment - and the tricks from this article !
# Let's talk !
Have any questions ? Would like to pair on Rust ? Curious about TDD ? Ping me !