So you’ve decided to write tests ❖ Is your code ready to test? ❖ Do you understand the testing tools? ❖ Do you understand some common testing patterns?
How To Control Dependencies ❖ Think of your program flow as “message passing” ❖ Refactor code to create required dependencies outside of where they are used ❖ No shame in incremental refactoring towards testability
Dependency Management As “Message Passing” ❖ Architect your application so results and dependencies flow through it ❖ Keep “side effects” to a minimum ❖ Give opportunities for tests to create doubles of dependencies that need to be in specific states
How To Detect Smelly Code ❖ Your tests require extensive setup steps ❖ Your code to set dependencies fills your editor screen ❖ It’s extremely difficult to tell if you’re getting the expected results
Extensive Setup Steps ❖ Does your app have complicated bootstrapping? ❖ Does it rely on hard-coded values for configuration options? ❖ How hard is it to swap out dependencies?
Avoid These ❖ conditional statements in your tests! ❖ loops in your tests! ❖ creating a test double of the thing you are testing just so “the damn thing works”
“There are too many examples of well-written tests and clear instructions for people to claim ignorance of how to write tests or use the tools to execute them.”
Database Connections? ❖ does your unit test REALLY need to make sure the database is working ❖ lets you control the expected responses in terms of result sets or record ID’s
3rd Party API’s? ❖ API use could be restricted (rate-limited, sandboxed for tests, pay-per-use) ❖ Again, are we in the business of testing their API or testing our code?