The Rails community has an intense focus on testing, and, specifically, TDD. This culture of testing is one of the best things about Rails. But the pressure to test, and test right, can be overwhelming for newcomers.
If you’re coming from a place where you weren’t testing at all, TDD can be especially hard to learn. Think about all the things you need to know in order to do TDD right:
- You have to know which features you want to implement
- You have to be able to identify which parts of each feature should be tested
- You have to learn a testing tool
- You have to know how to write some tests for each of those parts
- You have to know how to write a test without seeing the code first
- You have to know how to write the ‘simplest thing that could possibly work’
- You have to learn how to use your tests to grow the design of the code
When it’s written out like that, I’d be surprised if you didn’t struggle. It’s clear that you can’t really go from zero to TDD in a single step. So, what do you do? How can you learn TDD without being overwhelmed?
Break it apart!
When you feel like you’re facing an impossible task or you don’t know what to do next, see if there’s something easier you could learn that would get you closer to where you want to be.
For example, to learn TDD with RSpec, you could break it apart like this:
Write some exploratory code for a feature without tests. (So you can get some experience coming up with and writing features)
Study some code and write down some ways that the code might break on different kinds of input. (So you can learn about what to test and how to test)
Write tests for code that already exists, using the minitest built into Ruby (So you can learn minitest and basic testing, without having to learn TDD and RSpec, or fight with installing gems and getting your RSpec environment set up)
Test-drive some code using TDD and minitest (So you can focus on TDD without having to deal with gem dependencies or learning RSpec. Since you already know how to write tests, you just have to learn to write them first)
Test-drive some code using RSpec (once you’ve already practiced a bit with TDD, so you can focus on getting RSpec set up and learn its API and culture)
Each of these is a much smaller skill that you can practice separately. You can then focus on and practice the first step, then the next, and so on. Instead of jumping straight to TDD with RSpec, you can grow toward it.
It seems like a lot more work. But you’ll learn each of these skills faster than trying to learn TDD from scratch, and you’ll have a better foundation to build on when the next helpful testing style comes along.
Focus on one thing at a time
The key is to focus and practice on the thing just beyond your comfort zone, without trying to leap straight to the last step. Each of these steps has some value in itself. Even if you stopped after the first step, you’d still learn something valuable. But they also build on one another so you can focus on learning one thing really well, instead of having a bunch of things all thrown at you at once. You can learn without getting overwhelmed and giving up.
As you go through this process, give yourself permission to mess up. You’re learning, it’s totally OK! It’ll get a lot easier as you do it more often. Practice consistently and thoughtfully, and keep reaching one level beyond what you know already, and you’ll get there.