How thorough should you get with RSpec testing?
-
05-07-2019 - |
Question
I'm just starting to grasp BDD and RSpec and one thing I'm really having trouble with is figuring out how thorough I should be with my testing.
I'm just not understanding how fine-grained my testing should be to still be useful but not double development time.
Is it just a matter of preference? Or is there some general standard for what should be tested?
Solution
There's a few factors to consider here.
- Spec coverage should be greatest for the most important features and the things most likely to break.
- Specs should express developer intention. Don't write specs for trivial things you or someone else might change later.
- Test behavior not implementation. You should be able to change the internal implementation of a class and still pass the specs. This makes refactoring easier.
- Any time you fix bugs add specs to prevent the bug from regressing.
I find it helpful to think of Tests/Specs as a safety net. I want a spec to fail if another dev (or myself) breaks something that I spent time to make work. I don't want them to have to spend a lot of time changing specs for things that don't really matter. I also don't want to inhibit them from improving the application because of specs that test things that weren't important to me when writing the code.