views:

1159

answers:

5

I am familiar with the concepts (took testing classes in college), but I am not sure how to really use them yet since I never worked on a "real" TDD project.

I am about to start the development of a project using Ruby on Rails (most likely using 2.3). This application will be used to manage data, users and some files. It won't be too complicated at first but might scale a lot in the next 6 months so I feel this is the right time to get more into TDD.

I've got a basic idea on how to do it, but I still need some pointers and advices:

  • What Ruby on Rails TDD 101 article should I read?

  • What do I need to test?

  • What gem/plugin should I use?

  • Should I use rspec? Something else?

  • Once I've got all my testing classes, how do I go and deploy them?

  • How time consuming TDD really is?

  • 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?


I like learning with examples so could someone tell me how I would go and take a TDD approach to solve this issue:

I have Companies. I have Contacts. A contact can be linked to 1 company. A company can have multiple contacts. I want to create ways to create contacts, companies and link contacts to companies.

You don't have to use this example in your answer but it would help :)

+1  A: 

I recommend this book: Agile Web Development with Rails

JRL
+1  A: 

TDD is all about writing tests first. This basically forces you to write your own client before you write your application code. The cycle is generally write a test for an API that doesn't exist, run the test expecting it to fail, go write your API code, run your test again and make sure it passes. Then write your next test... and so on.

You might also be interested in this Rails guide.

Andy Gaskell
Thanks for the answer, the rails guide link is especially helpful
marcgg
+1  A: 

I use :

  1. Shoulda and rspec for testing
  2. Mocha for mocking
  3. Factory_girl for factories
  4. parallel_specs for fatser testing
  5. metric_fu for code analysis
Mike
+13  A: 

What Ruby on Rails TDD 101 article should I read?

I will start with a guide to testing rails applications, then read Test Driven Development 101. 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. Your 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 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, things need test 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
ez
thanks you for this really complete answer!
marcgg
+2  A: 

What gem/plugin should I use?

I've always enjoyed shoulda.

How time consuming TDD really is?

The reason I've always favored TDD development is that it focuses how I will implement a specific piece of code. I have an anecdotal feeling that whenever I adhere more strongly to TDD principles I spend less time reworking later. The amount of time spent is all in how well you write unit tests though. If the unit tests don't capture the expected behavior, all the time spent on them is wasted.

Patrick Robertson