How to get started on TDD with Ruby on Rails? [closed]

送分小仙女□ 提交于 2019-11-27 09:56:05
ez.

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!

I recommend this book: Agile Web Development with Rails

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.

Mike

I use :

  1. Shoulda and rspec for testing
  2. Mocha for mocking
  3. Factory_girl for factories
  4. parallel_specs for faster testing
  5. metric_fu for code analysis

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!