How to learn TDD with Ruby? How to learn TDD with Ruby? ruby ruby

How to learn TDD with Ruby?


The best way to learn TDD is by just doing it. I suggest you build a new project using TDD. This means don't write any non-testing code unless you have a failing test.

It will make you think about writing tests: I want to write this code, how do I write a test for it so I can write it.

It will show you the layered nature of testing. Instead of want a name that is required and can't contain numbers. you'll first test setting and reading a name, test requiring the name, test it shouldn't contain numbers, than think if it has more constraints and test those.

Remember:

  • Write a test before you write the code
  • Make sure the test fails! It's important to know you're testing logic is correct
  • Before writing the next test make sure all tests succeed
  • You can always clean up your code, if the tests keep working you didn't change the design


It's tricky getting your head around TDD (and BDD) but the book RSpec Book - BDD helped me a lot. Behaviour driven development is not exactly the same thing as TDD, but it is close and you have to think in a similar way.