why I need testings in rails?


I thought the same way and had the same questions for 4 months of writing Rails code before I finally saw the light. Now I realize that if I had been following a good testing methodology, those 4 months of code might have been completed in 6 weeks or so.

You say "I test my webapp all the time - code a page and then test it". What you are actually doing is 'debugging' - you're comparing the behavior of your app to the idea you have in your head of what the app is supposed to do. With testing, the focus gets much smaller - not 'the app is broken', but 'that last change made the user authentication fail because the password hash was other than expected'. You don't have to debug, you don't have to guess, you just have to fix the part that is broken, safe in the knowledge that your tests won't let you unknowingly introduce more bugs.

And here's the part that really blew my mind: once you figure out how testing works, you'll find yourself writing a test *before* you write the code that is being tested. This does 2 things - first it lets you know, the second you are done writing the code, whether what you wrote does what you intended or not, and second it lets you know when you're done writing the code. This doesn't mean you won't clean it up or refactor... in fact, testing means that you can refactor safely, because if the refactored code still passes the tests it has the same functionality as the original code. But in the meantime the tests let you know when the functionality of a section of code is complete, so you can move on to adding more functionality without getting bogged down in endless tweaks.

The 'autotest' part of ZenTest ( zentest | software projects | by ryan davis ) does *continuous* automated testing... every time you change a file, it runs the tests that are affected by that file. You get almost real-time feedback about what problems your changes have introduced... and not just "there is a problem"... if you wrote your tests correctly they will show you what broke on which line, and you can fix it immediately.

Read "Test First, by Intention" ( http://www.rubycentral.com/articles/pink/index.html ). Then if you want to see good tests in their native environment, look through technoweenie's code... Beast is a good starting place ( http://svn.techno-weenie.net/projects/beast/trunk/test/unit/ ). The Agile book ( http://pragmaticprogrammer.com/titles/rails2/index.html ) has a good chapter on testing that unfortunately, due to the constraints of the book format, doesn't show you the best testing methodology (they sort of tack it on at the end, reinforcing the perception that it is 'more work') but it is a good introduction to testing in Rails.

I know it sounds like more work, but I swear to you as a fellow doubter that it makes more sense than any other programming methodology I have ever seen. I code faster and more confidently than I did before I started testing, and I wouldn't even consider starting another project without testing.

- foobario