Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to learn TDD with Ruby?

Tags:

I have been using ruby exclusively for about a month and I really love it. However, I am having an incredibly hard time using, or even learning TDD. My brain just doesn't function that way...

I really, really want to learn TDD but to be honest I am a bit confused. All the articles that I find when Googling around are mostly specific to Rails, which is not interesting to me because I want to learn how to do efficient testing for any ruby application, from the simple one-file script to the complicated gem, not for Web apps. Also, there are so many frameworks and so few tutorials to get started.

Could anybody give me any advice on how to learn TDD so that I can at least start to consider myself an aspiring rubyist?

like image 634
Robert Audi Avatar asked Feb 07 '11 08:02

Robert Audi


People also ask

Is TDD difficult to learn?

Basically, TDD is hard! It needs skill, and it needs practice. The good news is that TDD rewards the effort. Once you get over the hurdle of working incrementally and writing fine-grained tests (hard), you'll find the implementation slots into place.

What is TDD in Ruby on Rails?

TDD in Ruby on Rails is a key practice in which the developer writes code first before writing the actual code to maintain the overall code quality.


2 Answers

Try RubyKoans.

like image 75
steenslag Avatar answered Sep 27 '22 22:09

steenslag


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
like image 34
Stefaan Colman Avatar answered Sep 27 '22 23:09

Stefaan Colman