How to get started on TDD with Ruby on Rails?
I recommend this book: Ruby on Rails Tutorial. I'm almost done with it. The book uses TDD the whole book. Give it a try!
I've produced a 6-episode video series which was taught as a public class in San Francisco in the summer of 2010. The material covers testing and developer efficiency in Rails 2.3 using RSpec 1.3. Slightly dated, but the main concepts apply to Rails 3 with Rspec 2.x
http://www.rubyfocus.biz/class_video/2010/07/19/rails_tdd_class_1.html
What Ruby on Rails TDD 101 article should I read?
I will start with a guide to testing rails applications.
Also Railscast has some excellent screencasts about how to use different testing tools.
What do I need to test?
I will start with models, since they are easy to test. The simple rule is that you need to cover every if statement in your test.
You should test the purpose of the method (to make sure it is functioning as expected) as well as all edge cases.
Also make sure you don't end up over testing.
What gem/plugin should I use? Should I use rspec? Something else?
When you start, just use Test Unit
. You can use rspec
or cucumber
after you get familiar with the basics.
Autotest
is a nice tool to have if you want to be truly test driven. But it is a 'nice have' not required.
Once I've got all my testing classes how do I go and deploy them?
Not sure about the question. You don't usually deploy the tests. Once you have all your testing classes simple type 'rake test' to run all your tests.
How time consuming TDD really is?
It saves time really. If you like labyrinth puzzle, you know it is almost always easier to solve it if you go from finish to start. Same with TDD. Without Test Driven you are consistently thinking 'what should i do next'. With Test Driven, the test will tell you what to do next (it breaks if the logic is not there so you just need to fix the broken part). Also you have less bugs which will save you a lot of time in the long run.
Do I need to read a book about this or can I get everything just by playing around with it and reading online tutorials? If I need to read a book, what book?
You do not need a book. The most efficient way of learning anything is: just do it. Go back to the book or online resources once you encounter a question or problem. This is agile too.
In your example, the things that need testing are: A contact can be linked to 1 company, A company can have multiple contacts, create ways to create contacts, and link contacts to companies.
class CompanyTest <Test::Unit
def test_relationship # test associations/relationships
c = companies(:some_company)
assert_equal [a list of contacts], c.contacts # make sure a company can have multiple contacts
end
end
class ContactTest<Test::Unit
def test_relationships
c = contact(:some_contact)
assert_equal some_company, c.company # make sure the contact link to 1 company
end
def test_create/add
# test create contacts, here you need to make sure the contact is created correctly, and linked to company correctly
end
end