How to get started on TDD with Ruby on Rails?

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? (eg: Continual Integration)

  • 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 :)


    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
    

    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


    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!

    链接地址: http://www.djcxy.com/p/45692.html

    上一篇: TDD与单元测试

    下一篇: Ruby on Rails如何开始使用TDD?