Plasticx Blog

Capable of being shaped or formed

Test Driven Development In Rails (TDD)

Posted by Mike 10/17/2006 at 11:38PM

I was talking to Geoffrey Grosenbach (of nuby on rails fame) at Seattle.rb tonight mostly about Typo and his new Peep Code screen casts. Somehow he told us that his next Peep Code screen cast was going to be on TDD. That was my opening to complain about actually writing the test first. To be frank, I put my foot in my mouth, and unit testing in Rails has changed the way I code and in turn has made me a better programmer.

I’ve read the “A Guide to Testing the Rails” at Ruby On Rails and so should you. While I was reading it I would go into the rails application I was working on
at the time and implement my testing. First the unit tests and then functional tests. Working on my code and my tests at the same has helped me to learn more features in the Rails framework to make my tests pass!

The rest of the HowtosTesting section in Ruby On Rails has other granular topics on Rails testing.

Tips (while in the root of your rails app):

$ rake test

Rake all tests.

$ rake test:units

Rake only unit tests.

$ rake test:functionals

Rake only functional tests.

$ ruby test/unit/foo_test.rb

Run a single unit test on the foo unit.

$ ruby test/functional/foo_controller_test.rb

Run a single functional test on the foo controller.

$ rake db:test:clone_structure

Clone the development DB to the test DB.

$ rake stats

Find out your code and tests stats (including Code to Test Ratio).

$ rake -T

List all the tasks that your rake can do.

Posted in , , , |


Use the following link to trackback from your own site:

  1. Hugh
    10/18/2006 at 06:55PM

    One of the features I like about Rails – tests are so easy to do. Plus the community has done a lot of work to make tests even easier to write. Check out Zentest if you haven’t already (autotest is amazing) and Mocha.

    I’m pretty excited to watch the next peepcode video on TDD (the one on REST was great).

  2. Eimantas
    02/21/2008 at 09:01AM

    Thanks for the TestingHowtos link .)

Web Statistics