- Makes more deliberate code
- Reason about system behavior before writing code
- Makes less production code
- We write the minimum amout of code to make the tests pass
- Less code = fewer places to introduce bugs
- Give reassurance in refactoring files
- Having tests lets developers not as familiar with the codebase be confident in making changes
- Helps to break down large problems
- Breaking down problems into smaller parts prevents coding constipation (fear of writing code because you don't know how to approach a problem)
- We want quick feedback from our unit tests - a couple seconds at most
- Tests shouldn't depend on each other
- Tests should validate an individual piece (unit) of code
- Tests should always be deterministic (given the same input, they should always have the same result)
- If you can't trust your tests, you can't trust your code
- You shouldn't need to manually check if the test passed or failed
- Write the tests as you go rather than writing all the tests, then writing all the code
- All of the data needed for the test should be created as part of the test
- Invoke the ACTUAL method under test
- Make assertions about the behavior/output
- A stand-in for a real object that provides no-ops for all methods
- Used for faking behavior of dependencies of entire class dependencies of our method under test
- A wrapper around an actual function that keeps the default behavior
- Watches (spies on) what the function does
- Check how many times a function was called
- Check what arguments were passed to it
- A stand-in for a function that fakes the behavior of the function
- A spy the redefines behavior
- Can also check function calls/arguments
- We define the return valus of the function
- Used for faking dependencies on individual functions of our method under test
- Can trigger pieces of code that wouldn't normally trigger (testing error handling)
- Replaces difficult to test code that isn't our method under test (database calls, network calls, etc.)
- Can also help out with async code
- When the code we are testing has side effects